Question About Finding Prime Numbers (Java)

Posted 02 October 2011 - 06:57 PM

I'm fairly new to programming, so please don't be too mean if my code is poorly structured or nonsensical!

I'm attempting problem 7 on Project Euler. It is asking for the 10,001th prime number. Now, in my head I have a good idea on how to tackle this one systematically, but I'm having a hard time translating this into code. My method for finding this prime number is this (please tell me if there are any flaws in my logic!):

Any number that is not prime can be broken up into smaller prime numbers. Thus, if some number n cannot be divided evenly into any of the prime numbers smaller than it, then n must be a prime number. With that being said, one could identify the 10,001th prime number by starting at 2, adding 1 (3), and checking to see if this number can be divided evenly by 2. Since 3 cannot be divided evenly by 2, it is also prime and can be added to the "prime list". Then the next number, 4, could be checked by dividing it by 2 and 3. Since it can be divided by at least one of these numbers (2), then it is not prime. Then 5 would be checked, and since it cannot be evenly divided by 2 or 3, it is prime and would be added to the list. This process would continue until 10001 prime numbers are present in the list, and the final one would be the answer.

I know that was probably an unnecessary explanation, but I wanted to give a clear explanation of my intention behind writing my code. So..here it is:

Alright, so here are my two questions..
1) Am I even approaching this correctly? If not, feel free to nudge me in the right direction. And by nudge, I do not mean "give me the answer". I'd like to figure this out on my own.
2) When I compile this program, it says that I can't "divide by zero" on the line "if(test%numberOfprimes[b] != 0){". Why is it doing that? When does numberOfprimes[b] ever equal zero?

Thanks in advance for taking your time reading this! And again, try not to judge me too harshly! I just started learning this a couple days ago, so I'm still very much a novice..
-Jacob

This post has been edited by jacobsnakob94: 02 October 2011 - 07:00 PM

Re: Question About Finding Prime Numbers (Java)

The variable b in your main method declaration isn't accessible inside that while statement. So it assumes that b is local inside that while loop and doesn't have a value.

By the way, why not use for loops?

Well, to answer your question, I'm kind of learning this on a "learn-new-things-as-I-need-them" basis. I just learned the basics and started doing problems on Project Euler from there, researching new things as I go. I didn't even know how to write arrays and such before this particular problem. So, I haven't looked into for-loops, but if they would provide a better alternative, then I'm more than willing to learn now!

Re: Question About Finding Prime Numbers (Java)

Posted 02 October 2011 - 07:35 PM

I used the Sieve of Eratosthenes to do this, and just generated additional numbers if I found myself lacking. So if I found all the primes between 2-10000 and found myself only having 1000 primes, I'd add numbers from 10001-12000 (as an example) to the List and sieve those. Repeating until I had 10001 primes. You may find an ArrayList helpful here.

Re: Question About Finding Prime Numbers (Java)

Posted 02 October 2011 - 07:44 PM

macosxnerd101, on 02 October 2011 - 07:35 PM, said:

I used the Sieve of Eratosthenes to do this, and just generated additional numbers if I found myself lacking. So if I found all the primes between 2-10000 and found myself only having 1000 primes, I'd add numbers from 10001-12000 (as an example) to the List and sieve those. Repeating until I had 10001 primes. You may find an ArrayList helpful here.