and if so can you explain just the concept or theory of it? I'm having lots of fun implementing the code and want to write it myself.
static void makeComposite(int[] primeBools, int x) {
primeBools[x/64] |= (1 << ((x >> 1) & 31));
}
public static int isComposite(int[] primeBools, int x) {
return (primeBools[x/64] & (1 << ((x >> 1) & 31)));
}
public static long primeSieve(int n) {
int estimatedLimit = (int)((Math.log(n) + Math.log(Math.log(n)) - 1 + ( (Math.log(Math.log(n)) - 2) / Math.log(n) )
- (( Math.pow(Math.log(Math.log(n)),2) - 6 * Math.log(Math.log(n)) - 11 )/( 2 * Math.pow(Math.log(n),2) ))) * n);
int[] isPrimeBools = new int[estimatedLimit/64 + 1];
Arrays.fill(isPrimeBools, 0000);
for (int i = 3; i * i <= estimatedLimit; i+=2) {
if (isComposite(isPrimeBools, i) == 0) {
for (int j = i*i, k = i << 1; j < estimatedLimit; j += k) {
makeComposite(isPrimeBools, j);
}
}
}
int finalPrime = 1;
for (int foundPrimes = 1; foundPrimes <= n; finalPrime += 2) {
if (isComposite(isPrimeBools, finalPrime) == 0) {
foundPrimes++;
}
}
return n == 1 ? 2 : finalPrime-2;
}