6.8 Enumerate all primes to n

#1

for (long j = ((i * i) * 2) + 6 * i + 3; j < size; j += p) {

Why do we add p instead of (p-3)/2?

Edit: Ah nevermind. It’s because we want the next value to eliminate to be p^2 + 2p instead of p^2 + p because we want an odd value. And adding p to the index is like adding 2p to the value.

0 Likes

#2

Hi @fondant,

This is a super optimized code but we do have comments in code explaining how it goes. Feel free to let us know if you have any problem.

0 Likes

#3

Hi,

Thanks for the response.
I just checked the book again and there’s a comment explaining j = ((i * i) * 2) + 6 * i + 3, but no comment that explains j += p.

0 Likes

#4

Hi @fondant,

You are right as we only have comment about why j is as j += p is not hard to understand once you figure out what j is.

0 Likes