473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

A Judagrali series

13,262 8TB
A while ago the following question was flooding programming forums.

Find the 4th prime number N such that N=NumberOfDigits(Q) where N is prime AND Q is
prime AND where Q = (2^p) - 1 where P is prime.
We are looking for the 2nd,3rd, 4th prime number N in the Judagrali series as well as its
corresponding Q and P values.
Hints:
1 The 4th N corresponds to a very large Q. Take this into account when doing the
calculation.
2 Computational efficiency is important.
3 Object orientation and inheritance MUST be used. i.e. N is a prime, and a subset
(subclass) of Q, Q is also prime and a subset of P which is also prime.




It was apparently being asked in interview questions.
The hints were complete giveaways.

1 The 4th N corresponds to a very large Q. Take this into account when doing the
calculation.
That's an easy one. Whatever variable that we use to store our Q's needs to be able to store large integers (I'll use java.math.BigInteger).

2 Computational efficiency is important.
I'll just use java.math.BigDecimal's nextProbablePrime method for generating primes.

3 Object orientation and inheritance MUST be used. i.e. N is a prime, and a subset
(subclass) of Q, Q is also prime and a subset of P which is also prime.
Starting with the relationship between P and Q, P is just a (prime) number.

Expand|Select|Wrap|Line Numbers
  1. class P {
  2.      BigInteger value;
  3.         public P(BigInteger p) {
  4.           value = p;
  5.         }
  6.      public P() {}
  7. }

As you can see, P is not interesting at all. It just stores the BigInteger value in a, well, value variable. The only important thing about it is that it must store prime numbers only. The isJudagrali method just tests if value is a prime number.
Now Q and P's relationship is specified as Q = (2^p) - 1. From that relation, if we have a P, we can get a Q easily. However, for that Q to be a valid number for our sequence, it (the Q) needs to be prime as well. The fact that it needs to be prime as well means that the Q class can extend P. Our isJudagrali method inherited from P still works for us here. The relationship Q = (2^p) - 1 is easily enforced in Q's constructor.


Expand|Select|Wrap|Line Numbers
  1. class Q extends P {
  2.     public Q() {}
  3.     public Q(P p) {
  4.         value = BigInteger.ONE.shiftLeft(p.value.intValue())
  5.    .subtract(BigInteger.ONE);
  6.     }
  7. }
I just did a little bit of shifting to achieve (2^p). There is nothing evil about it!
The last piece of the puzzle is the N.
N is a Q so we make it extend Q. It should also hold the number of digits of a given Q.
Again the constructor can enforce that for us:


Expand|Select|Wrap|Line Numbers
  1. class N extends Q {
  2.     public N(Q q) {
  3.         value = numDigits(q.value);
  4.     }
  5.     public BigInteger numDigits (BigInteger num) {
  6.          String s = ""+ num;
  7.          return new BigInteger(""+s.length());
  8.     }
  9. }


The prime number generator I'll use for this one is simply from the BigInteger class


Expand|Select|Wrap|Line Numbers
  1. class PrimeGenerator {
  2.     BigInteger current = BigInteger.ONE;
  3.     public BigInteger next() {
  4.        current = current.nextProbablePrime();
  5.        return current;
  6.     }
  7. }

The main program doesn't have much to do. It simply grabs primes from our prime number generator, constructs a P and it's corresponding Q value. If the Q and P relationship is satisfied, it constructs
a corresponding N using that Q. If N is valid Judagrali then it prints out the P, Q, N values until 4 of them have been printed.

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) {
  2.  PrimeGenerator g = new PrimeGenerator();
  3.  int count = 0;
  4.  while (count < 4) {      
  5.   P p = new P(g.next());
  6.   Q q = new Q(p);
  7.   if(q.isJudagrali()) {
  8.     N n = new N(q );
  9.     if(n.isJudagrali()) {
  10.       System.out.println("P :" +p.value + " Q: "+q.value + "  N :"+n.value);
  11.       count++;
  12.     }
  13.   }
  14.  }
  15. }
  16.  
There is no need to test if P is Prime because, hopefully, our prime generator is correct!
Nov 6 '08 #1
2 4352
jkmyoung
2,057 Expert 2GB
Considering this, do we actually need to store Q?

The relationship from p to n is
n = Roundup(log(2)*p)
and we can store log(2) as a constant.

Testing if 2^p -1 is prime may be easier than testing if a particular number is prime because we can use modulus.


If using the brute force method to test if something is prime:
2^p-1 is divisible by x, only if
2^p - 1 = 0 mod x
2^p = 1 mod x
This should be computationally faster, eg take the binary of p, and find
2^1 mod x
2^2 mod x
2^4 mod x
...
2^(2^n) mod x for some n, where p < 2^n
add up the relative terms mod x, and see if they add to 0.

Of course, this all depends on how we test for primality.
Dec 10 '08 #2
r035198x
13,262 8TB
Sounds right. I hadn't realized that. Hint 3 though seems to want the solution to contain Q and have the relationships set up.
Dec 17 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
6
by: Daniel Kabs | last post by:
Hello there, I have a Nokia 6630 phone that is based on the Series 60 Software Platform. -> http://www.series60.com/ These "smartphones" are said to support HTML 4.01 and "a subset of...
4
by: aW | last post by:
I have an interesting dilemma. I have a table with the following records: =================================================== Box | Series Start | Series End...
1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
4
by: geoffp | last post by:
I need to generate reproducible random number series. I've done the obvious - use mt_srand with the same seed. This supposedly will create the same series every time. Is this true? Its not...
33
by: r034802n | last post by:
A Judagrali series: Find the 4th prime number N such that N=NumberOfDigits(Q) where N is prime AND Q is prime AND where Q=2p-1 where P is prime. Example the 1st prime number N is 2, because...
4
by: keirnus | last post by:
Hello, I've been coding in Access VBA to create an Excel Graph and it was good. Until I got this error: Please check the code below: Private Sub TestGraph3()
1
by: Scholar | last post by:
Hi,i am working on a c++ project which will find the nth term of a given series.The series would be given in the form of first 4 or 5 terms.I have the basic alogorithm which can find the nth term of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.