Results 1 to 16 of 16
Thread: Boolean method help
 04242012, 07:17 PM #1Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Boolean method help
The Challenge:
What is the 10 001st prime number?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; } } }
I want x to count upwards, and each time x increases by 1, I want y to go from 2 to x1 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
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; 04242012 at 08:18 PM. Reason: Added questions
 04242012, 08:00 PM #2
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.
 04242012, 08:24 PM #3
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 04242012, 08:29 PM #4
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.
 04242012, 08:44 PM #5Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Re: Boolean method help
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
 04242012, 08:49 PM #6
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.
 04242012, 09:02 PM #7Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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
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.
 04242012, 09:06 PM #8
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.
 04242012, 09:22 PM #9Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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 != a1){ return false; } else { return true; } } }
y: 2x: 3Java 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!=x1) { System.out.println("y: " + y + "x: " +x); } else { break; } } } }
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: 2029Last edited by Zigster; 04242012 at 09:23 PM. Reason: clarity
 04242012, 09:24 PM #10
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.
 04242012, 09:32 PM #11Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Re: Boolean method help
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 am letting in x%y!=0 to my if statement where it prints x.Last edited by Zigster; 04242012 at 09:33 PM. Reason: clarity
 04242012, 09:39 PM #12
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 x1 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.
 04242012, 10:11 PM #13Member
 Join Date
 Apr 2012
 Posts
 44
 Rep Power
 0
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; 04242012 at 10:14 PM.
 04242012, 11:43 PM #14Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
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==x1.
@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; 04242012 at 11:46 PM.
 04242012, 11:48 PM #15
Re: Boolean method help
You've changed the definition of PrimeN.
Last edited by Norm; 04242012 at 11:53 PM.
If you don't understand my response, don't ignore it, ask a question.
 04242012, 11:52 PM #16Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Re: Boolean method help
Similar Threads

Boolean Method Problem
By Bonfy in forum New To JavaReplies: 32Last Post: 01232012, 02:21 AM 
Boolean setter Method
By JonoHart in forum New To JavaReplies: 8Last Post: 01192011, 06:37 PM 
Boolean and Method help on Homework
By gto400no1 in forum New To JavaReplies: 3Last Post: 02222010, 01:12 AM 
Boolean method help
By syferite in forum New To JavaReplies: 6Last Post: 10282009, 02:32 PM 
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02222009, 03:36 AM
Bookmarks