# returning a prime number from method

• 10-17-2012, 07:14 PM
tabako
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.
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);         }```
So i was asked to get a random number between 3 and 128, which my randomNumber variable does. I then try to make a loop trying to make sure it is a prime number and then return the number. Any help would be appreciated.
• 10-17-2012, 07:25 PM
arieltal
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.
• 10-18-2012, 05:58 AM
tabako
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.
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```
My code looks like the following:
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;         }```
It could be that I don't understand the pseudo completely, but i think i have most of what was written down and close to correct, so any steps in the right direction would be helpful.