Results 1 to 3 of 3
- 10-17-2012, 07:14 PM #1
Member
- 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.
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.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); }
- 10-17-2012, 07:25 PM #2
Member
- 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.
- 10-18-2012, 05:58 AM #3
Member
- 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.
My code looks like the following: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
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.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: 02-17-2011, 09:13 AM -
displaying prime number
By eng_hyzoom in forum New To JavaReplies: 1Last Post: 11-26-2010, 11:21 AM -
Finding nth prime number
By dextr in forum New To JavaReplies: 2Last Post: 04-12-2010, 11:42 PM -
Returning the Greatest Prime Factor
By BJ1110 in forum New To JavaReplies: 15Last Post: 10-23-2009, 10:06 PM -
Prime Number - System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04-26-2009, 01:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks