while (p*p < s) { // repeat until p^2 > size of the ORIGINAL set
for (int i = 0; i <= b.length(); i++) {
if (i%p == 0) { // if it's a multiple of p
b.set(i, false); // set it to false (not prime)
}
}
p = b.nextSetBit(p);
// Make p = first bit that is set to true starting after the previous p index.
}
Marks all multiples of the prime p
as composite, including p
itself. So only the primes larger than the square root of the limit remain unmarked.
- This method uses the Sieve of Eratosthenes
No, it doesn't. It uses trial division. You divide each number by all primes smaller than the square root of the limit and check whether the remainder is 0.
The loop should be
for(int i = p*p; i < b.length; i += p)
b.set(i,false);
to implement the Sieve of Eratosthenes.