Results 1 to 16 of 16
  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Boolean method help

    The Challenge:
    What is the 10 001st prime number?
    My Code:
    Java Code:
    public class Problem7 {
    	public static void main (String[] args){
    	int x, y;
    		for (x=2;x>0;x++){
    			for (y=2;y<x;y++)
    			{
    				if (PrimeN(x, y))
    				{
    				System.out.println(x);
    				}
    				else
    				{
    				break;
    				}
    			}
    		}	
    	}
    	
    	
    	public static boolean PrimeN(int a, int b){
    	
    		if (a==2 || a==3){
    			return true;
    		}
    		if (a%b==0){
    			return false;
    		}
    		else
    		{
    			return true;
    		}
    	}
    }
    Finding this one tricky, and first time using boolean and multiple methods.

    I want x to count upwards, and each time x increases by 1, I want y to go from 2 to x-1 to see if there are any factors, so I can sort out the prime numbers. Then each time there is a prime number, i'll make a varible that ++ until it gets to 10001, then I will print the answer and terminate the program.

    Atm my code prints out:
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    4690
    Must print out each number about 100 times, and they're not prime numbers.

    Questions:

    Why is it printing out x as the same number approx one hundred times before it goes to the next number?

    Why is my PrimeN boolean not sorting Prime numbers at all?

    Is my basic concept above going to actually going to get the answer, or have I got it totally wrong?

    Cheers!
    Last edited by Zigster; 04-24-2012 at 07:18 PM. Reason: Added questions

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    Do you have a question about the posted code?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Boolean method help

    Zigster, please don't edit your posts after there are responses, as that breaks the context and in extreme cases can make a thread barely readable.

    It would have been better if you replied to Norm's post in a new response in the thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    Try debugging the code by printing out the values of x AND the value of y to seen what it is doing in the loops.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean method help

    Quote Originally Posted by DarrylBurke View Post
    Zigster, please don't edit your posts after there are responses, as that breaks the context and in extreme cases can make a thread barely readable.

    It would have been better if you replied to Norm's post in a new response in the thread.

    db
    Ok, I will remember for the future, sorry.

    I printed Y as suggested:
    X 1483
    Y 957
    X 1483
    Y 958
    X 1483
    Y 959
    X 1483
    Y 960
    X 1483
    Y 961
    X 1483
    Y 962
    X 1483
    Y 963
    X 1483
    Y 964
    X 1483
    Y 965
    X 1483
    Y 966
    X 1483
    When it hits a factor of x, it starts the loop again. So is my boolean working then? Why is x%y!=0 coming through my if statement if y is recognising the (a%b==0){return false} from the boolean method?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    What does the PrimeN method tell you when it is called?
    What is it you want to know about the values of x and y that are passed to the PrimeN method?

    The print out would be easier to read and understand if you printed the values of x and y on the same line.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean method help

    Sorry, I don't understand when you say: "What does the PrimeN method tell you when it is called?"?

    It's supposed to sort x into prime numbers.

    I printed y and x together like you said:

    y: 38x: 2173
    y: 39x: 2173
    y: 40x: 2173
    y: 2x: 2175
    y: 2x: 2177
    y: 3x: 2177
    y: 4x: 2177
    y: 5x: 2177
    y: 6x: 2177
    y: 2x: 2179
    y: 3x: 2179
    y: 4x: 2179
    y: 5x: 2179
    Using a calculator:

    2173 / 41 = 53
    2174 / 2 = 1087
    2175 / 3 = 725
    2176 / 2 = 1088

    So it resets when x%y==0, which is good, but I want it to filter out before the if so they don't print.

    I don't want x to get through the if statement when x%y!=0, I'm trying to do it using a boolean.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    What numbers are you printing out? Why are those printed and the others not printed?
    The print out shows y is changing on every line but x does not change every line. What does that tell you about what the code is doing?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean method help

    I am printing out x and y, x is counting upwards when y reaches x%y==0, x does not change until x%y==0.

    I need to... change my if statement. I want to keep out x%y!=0, until it reaches x%y==0 (and then break) , or until it y reaches x - 1 (and then print x).

    I tried this:

    Java Code:
    public static boolean PrimeN(int a, int b){
    	
    		if (a==2 || a==3){
    			return true;
    		}
    		if (a%b==0){
    			return false;
    		}
    		if (b != a-1){
    			return false;
    		}
    		else
    		{
    			return true;
    		}
    	}
    }
    And it prints:
    y: 2x: 3
    I tried this:
    Java Code:
    public static void main (String[] args){
    	int x, y;
    		for (x= 2; x > 0; x++){
    			for (y = 2; y < x; y++)
    			{
    				if (PrimeN(x, y) && y!=x-1)
    				{
    				System.out.println("y: " + y + "x: " +x);
    				}
    				else
    				{
    				break;
    				}
    			}
    		}	
    	}
    And it prints:y:
    956x: 2029
    y: 957x: 2029
    y: 958x: 2029
    y: 959x: 2029
    y: 960x: 2029
    y: 961x: 2029
    y: 962x: 2029
    y: 963x: 2029
    y: 964x: 2029
    y: 965x: 2029
    y: 966x: 2029
    y: 967x: 2029
    y: 968x: 2029
    y: 969x: 2029
    y: 970x: 2029
    y: 971x: 2029
    y: 972x: 2029
    y: 973x: 2029
    y: 974x: 2029
    y: 975x: 2029
    y: 976x: 2029
    Same as before.
    Last edited by Zigster; 04-24-2012 at 08:23 PM. Reason: clarity

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    The output looks correct. Can you explain what is wrong with it?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean method help

    Quote Originally Posted by Norm View Post
    The output looks correct. Can you explain what is wrong with it?
    This Output?
    956x: 2029
    y: 957x: 2029
    y: 958x: 2029
    y: 959x: 2029
    y: 960x: 2029
    y: 961x: 2029
    y: 962x: 2029
    y: 963x: 2029
    y: 964x: 2029
    y: 965x: 2029
    y: 966x: 2029
    y: 967x: 2029
    y: 968x: 2029
    y: 969x: 2029
    y: 970x: 2029
    y: 971x: 2029
    y: 972x: 2029
    y: 973x: 2029
    y: 974x: 2029
    y: 975x: 2029
    y: 976x: 2029
    I only want to print x if it y has gone from 2 to x-1 without finding any factors in x.

    I am letting in x%y!=0 to my if statement where it prints x.
    Last edited by Zigster; 04-24-2012 at 08:33 PM. Reason: clarity

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    Then you need to change the code to do that. Your code prints a line every time PrimeN returns true. Look at all the lines that were printed. PrimeN returned true for all of them.

    When do you want to print the line showing the value of x? You say: if y has gone from 2 to x-1 without finding any factors in x.
    That means you should print once at the end of the loop after all those tests have been made.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    kosmos890 is offline Member
    Join Date
    Apr 2012
    Posts
    40
    Rep Power
    0

    Default Re: Boolean method help

    This is the simplest but also the slowest algorithm. Search google for a good algorithm such Sieve of Eratosthenes and try to implement
    Java Code:
    public class Main{
    	
    	public static void main(String[] args){
    		
    		int counter=1;
    		int num=2;
    
    		while(counter<10001){
    			
    			num++;
    			if(isPrime(num)){
    				counter++;
    			}							
    		}		
    		System.out.println(counter+"th prime number is "+num);
    	}
    
    	public static boolean isPrime(int num){
    		
    		if(num==1)
    			return false;
    
    		for(int divisor=2; divisor*divisor<=num;divisor++)
    			if(num%divisor==0)
    				return false;		
    
    		return true;
    	}
    }
    Last edited by kosmos890; 04-24-2012 at 09:14 PM.

  14. #14
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean method help

    @Norm: Cheers, it was a problem with my boolean and y wasn't counting up properly so it was never getting to y==x-1.

    @kosmos: Thanks, it's much cleaner than my method.

    Got it working, outputs all prime numbers to the 10001th prime number. Thank you all for the help.

    Java Code:
    public class Problem7 {
    	public static void main (String[] args){
    	
    		int x, y, z;
    		System.out.println("Prime numers:");
    		z=0;
    				for (x= 2; z < 10001; x++)
    				{
    					y=2;
    					outerloop:
    					do 
    					{
    						if (PrimeN(x, y))
    						{
    						break outerloop;
    						}
    						y++;
    					}
    					while (y<x);
    					if (x==y)
    					{
    					System.out.println(z + "\t" +x);
    					z++;
    					}
    				
    				if (z==10001){
    					System.out.println("The " + z + "Prime Number is: " + x);
    				}
    				}
    			}	
    	
    	
    	public static boolean PrimeN(int a, int b){
    
    		if (a%b==0){
    			return true;
    		}
    
    		else
    		{
    			return false;
    		}
    		
    	}
    }
    Last edited by Zigster; 04-24-2012 at 10:46 PM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Boolean method help

    You've changed the definition of PrimeN.
    Last edited by Norm; 04-24-2012 at 10:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: Boolean method help

    Quote Originally Posted by Norm View Post
    You exit the loop the first time (a%b==0) it true. Does that give you the results you want?
    Yes, I want to exit the loop if a%b==0 because if a%b==0 a cannot be a prime number.

    This is an example of what it prints:
    9827 102647
    9828 102653
    9829 102667
    9830 102673
    9831 102677
    9832 102679
    9833 102701
    Numbers on right are prime numbers.

    Cheers!

Similar Threads

  1. Boolean Method Problem
    By Bonfy in forum New To Java
    Replies: 32
    Last Post: 01-23-2012, 01:21 AM
  2. Boolean setter Method
    By JonoHart in forum New To Java
    Replies: 8
    Last Post: 01-19-2011, 05:37 PM
  3. Boolean and Method help on Homework
    By gto400no1 in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 12:12 AM
  4. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 01:32 PM
  5. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 02:36 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
  •