Results 1 to 3 of 3
 10172012, 07:14 PM #1Member
 Join Date
 Oct 2010
 Posts
 8
 Rep Power
 0
returning a prime number from method
So the first part of my program that it should be doing is generating two random prime numbers.
I have been given pseduo code to help which was give as the following:
Do until a prime is found
randomly select a number between 3 and 128
loop from i equal to 2 to P  1 and increment i by one each time
if P mod i is zero
break; // it isn't prime, no need to continue
if i is greater than or equal to P  1 then P is prime
My current method looks like this but when i try to output the prime numbers it doesn't work.
Java Code:public static double pickPrime() { double randomNumber = 0.0; do { randomNumber = (int) (3 + Math.random() * 125); int i = 2; while (i < randomNumber  1) { if (randomNumber % i == 0) { break; } if (i >= randomNumber  1) { return randomNumber; } i++; } } while (true); }
 10172012, 07:25 PM #2Member
 Join Date
 Sep 2012
 Posts
 34
 Rep Power
 0
Re: returning a prime number from method
the problem is in line 7, just choose a number and follow the debugging to see why it's infinite.
 10182012, 05:58 AM #3Member
 Join Date
 Oct 2010
 Posts
 8
 Rep Power
 0
Re: returning a prime number from method
ok, so i got the getPrime method working.
So then, i put P and Q into static ints vai that method(so: private static int P = (int)pickPrime();
So now what the program is supposed to do is find a number(in this case we'll call it E), that is relatively prime.
I have also pseduo code to work this method.
Java Code:// For e values starting at 2 up to phiPQ  1, check each to see if e is a gcd of phiPQ // assign phiPQ to a variable named larger and e to a variable named smaller. // initialize a variable named r to 1 (anything over zero) // do the following until r reaches zero // compute the remainder, r, of dividing larger by smaller. // replace larger with smaller and smaller with the computed r value. // after r reaches 0, the GCD value is the one stored in larger // if GCD is 1 then you've found an e that is relatively prime to phiPQ
Java Code:public static int findE() { int a =0; for (int e = 2; e <= phipq  1; e++) { int larger = phipq; int smaller = e; int r = 1; do { r = larger % smaller; larger = smaller; smaller = r; a=e; } while (r != 0); } return a; }
Similar Threads

Prime Number finder
By hoosierfan24 in forum New To JavaReplies: 11Last Post: 02172011, 09:13 AM 
displaying prime number
By eng_hyzoom in forum New To JavaReplies: 1Last Post: 11262010, 11:21 AM 
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04122010, 11:42 PM 
Returning the Greatest Prime Factor
By BJ1110 in forum New To JavaReplies: 15Last Post: 10232009, 10:06 PM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM
Bookmarks