This content has been marked as final.
Show 19 replies

1. Re: Java help please.. prime numbers
807599 Nov 12, 2006 11:49 PM (in response to 807599)do you mean to say that you dont know how to test if a number is prime or not?! 

3. Re: Java help please.. prime numbers
807599 Nov 13, 2006 12:05 AM (in response to 807599)A number is prime if it is only divisible by one and itself. So, If it is not divisible by another number between 1 and itself, then it is prime. 
4. Re: Java help please.. prime numbers
807599 Nov 13, 2006 12:17 AM (in response to 807599)This code should work:
Although I can't test it right now.bool isPrime(int numtotest) { for (int i = 2; i < numtotest; i++) { if (numtotest % i == 0) { return false; } } return true; }

5. Re: Java help please.. prime numbers
807599 Nov 13, 2006 12:18 AM (in response to 807599)
I dont know what to fill in for %WholeNumber%. It would have to be some sorta hole number checker. Or u can do it backwards and do some sorta decimal checker. Not sure on how to make those checkers.if(the_number / 2 != %WholeNumber%); { System.out.println("Your number is prime... NO SOUP FOR YOU!!!"); } else { System.out.println("Your number is even, you get a cookie! :D"); }

6. Re: Java help please.. prime numbers
807599 Nov 13, 2006 12:19 AM (in response to 807599)lol, i took too long to type, answer for what goes in the if parentheses is up there. 
7. Re: Java help please.. prime numbers
807599 Nov 13, 2006 12:49 AM (in response to 807599)To make the code more efficient only loop until the square root of the number to test. 
8. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:15 AM (in response to 807599)is there even a way to put something as "whole numbers" 
9. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:19 AM (in response to 807599)What do you mean by "whole numbers"?
Use the code provided by JimmyM but end the loop when you reach the square root of the number rather than the number. 
10. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:23 AM (in response to 807599)sorry you lost me there... a little confused now 
11. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:27 AM (in response to 807599)sorry you lost me there... a little confused now
I wouldn't worry about it, use JimmyM's code. It will be slower for big numbers but it will work fine. 
12. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:29 AM (in response to 807599)If you don't understand and don't care just use the provided code as is.
If you do care, read on.
If the number is 24 it has a square root of approx 4.8 and the factors are:
1 * 24
2 * 12
3 * 8
4 * 6
All the factors on the left are less than 4.8 and all the factors on the right are > 4.8. So if you haven't found a factor for a given number before you hit the square root, you won't find a factor so you can stop looking. It makes your algorithm slightly more efficient. 
13. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:32 AM (in response to 807599)hmm i see... i'll see if i can understand this completely.. and by the way yes i do care!! heh 
14. Re: Java help please.. prime numbers
807599 Nov 13, 2006 2:37 AM (in response to 807599)so okay i'm trying to figure this out... if say i ask for 8 if it's a prime how would that work.. just trying to figure out the theory of your way..