You have three problems with your code:
- Your code does not test whether 2 is a prime, so you are missing that in your count. You should initialize
counter
to 0 or 1, not 2. - By using
while (i<=10001)
, you are counting until you find 10002 primes. Since you are also not counting 2, you are going two steps past where you need to go. The loop test should bewhile (i<10001)
. - Since your answer is lower than the correct answer (even though your loop went two primes past where it should have stopped), your
Helper.isPrime
method is clearly identifying too many numbers as prime. It will need to be fixed, but since you have not posted the code for it, it’s impossible to say what is needed to fix it.
solved Calculating the 10001st prime number [closed]