# Boolean method help

• 04-24-2012, 06:17 PM
Zigster
Boolean method help
The Challenge:
Quote:

What is the 10 001st prime number?
My Code:
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:
Quote:

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!
• 04-24-2012, 07:00 PM
Norm
Re: Boolean method help
Do you have a question about the posted code?
• 04-24-2012, 07:24 PM
DarrylBurke
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
• 04-24-2012, 07:29 PM
Norm
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.
• 04-24-2012, 07:44 PM
Zigster
Re: Boolean method help
Quote:

Originally Posted by DarrylBurke
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:
Quote:

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?
• 04-24-2012, 07:49 PM
Norm
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.
• 04-24-2012, 08:02 PM
Zigster
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:

Quote:

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.
• 04-24-2012, 08:06 PM
Norm
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?
• 04-24-2012, 08:22 PM
Zigster
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:

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:
Quote:

y: 2x: 3
I tried this:
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:
Quote:

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.
• 04-24-2012, 08:24 PM
Norm
Re: Boolean method help
The output looks correct. Can you explain what is wrong with it?
• 04-24-2012, 08:32 PM
Zigster
Re: Boolean method help
Quote:

Originally Posted by Norm
The output looks correct. Can you explain what is wrong with it?

This Output?
Quote:

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.
• 04-24-2012, 08:39 PM
Norm
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.
• 04-24-2012, 09:11 PM
kosmos890
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
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;         } }```
• 04-24-2012, 10:43 PM
Zigster
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.

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;                 }                         } }```
• 04-24-2012, 10:48 PM
Norm
Re: Boolean method help
You've changed the definition of PrimeN.
• 04-24-2012, 10:52 PM
Zigster
Re: Boolean method help
Quote:

Originally Posted by Norm
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:
Quote:

9827 102647
9828 102653
9829 102667
9830 102673
9831 102677
9832 102679
9833 102701
Numbers on right are prime numbers.

Cheers!