The problem appears to be that you're not checking for division by 2.
You're calling crossOut(16)
at some point (specifically as a multiple of 4
), which crosses out the corresponding value for 17
. Similarly for 112
causing 113
to be crossed out.
Change:
if (i != curr && i % curr == 0) {
to:
if (i % 2 != 0 && i != curr && i % curr == 0) {
and both of those gets printed as prime.
Also, I'm a bit concerned with what exactly that loop is doing - I didn't look at the output in too much detail, although it actually appears to be all the primes, but I imagine you should looping over multiples of curr
, the first being 2*curr
(not curr*curr
), the increment between being curr
(not 2*curr
), although I'm not saying what you've done is incorrect, as I need a bit more time to understand exactly how your code is solving this problem.