Results 1 to 3 of 3
  1. #1
    tabako is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default 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) {
    				if (i >= randomNumber - 1) {
    					return randomNumber;
    		} 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.

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Rep Power

    Default 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.

  3. #3
    tabako is offline Member
    Join Date
    Oct 2010
    Rep Power

    Default 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
    My code looks like the following:
    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;
    			} 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.

Similar Threads

  1. Prime Number finder
    By hoosierfan24 in forum New To Java
    Replies: 11
    Last Post: 02-17-2011, 09:13 AM
  2. displaying prime number
    By eng_hyzoom in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 11:21 AM
  3. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  4. Returning the Greatest Prime Factor
    By BJ1110 in forum New To Java
    Replies: 15
    Last Post: 10-23-2009, 10:06 PM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts