|
-
August 4th, 2009, 05:57 PM
#1
prime number generator
Hi all,
I am attempting to teach myself java from a tutorial webpage. One of the exercises is to write a program to calculate the first 100 prime numbers. Here's what I came up with:
Code:
class Prime {
public static void main(String[] arguments) {
int number = 2;
int primes = 1;
System.out.println("Prime #" + primes + " is " + number);
number++;
primes++;
// keep a count of prime numbers
while (primes <= 100) {
// test each number for primeness
for (int divisor = 2; divisor < number; divisor++) {
if (number % divisor == 0) {
number++;
continue;}
}
// print prime numbers
System.out.println("Prime #" + primes + " is " + number);
number++;
primes++;}
}
}
The program compiles just fine. When I ran it, it appeared to run just fine... until I realized that not all the numbers it's returning as prime actually are. It lists 2, 3, 5, 7, 11, 13, 17, 19, 23 (so far so good), 27(?), 29, 31, 35(?), 37, 41, and so on. Every once in a while a composite number sneaks into the list and I have no idea why. Browsing other threads, it's clear from other people who have worked on similar programs that there are more efficient ways of doing it (for instance, since 2 is the only even prime, I only really need to test odd numbers) but it still seems like my program should work. Can anyone help?
Jeff
-
August 4th, 2009, 06:26 PM
#2
Re: prime number generator
Because inside IF: number variable changes and divisor stays on the same position it was left by the previous number value until number variable checks as a "prime" number.
For example we try number = 24: by the time number equals 27 divisor is already 14. So for number=27 we run divisor from 14 till 26 and 27 never is fully divided so it gets into the list of primes that is obviously wrong.
Reinitialize divisor every time you change the number.
It's all can be clearly seen using any debugger...
Last edited by Xeel; August 4th, 2009 at 06:33 PM.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
August 4th, 2009, 06:57 PM
#3
Re: prime number generator
To speed it up, you can try dividing only by odd numbers less than half the target number. To speed it up even more, store the primes already calculated and divide only by them (again for values up to half the target). And so-on ;-)
Doing more things faster is no substitute for doing the right things...
S. R. Covey
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
August 4th, 2009, 10:34 PM
#4
Re: prime number generator
Xeel,
That was indeed the problem. Thanks very much.
Jeff
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|