Created
October 11, 2021 19:32
-
-
Save vitaly-t/efe4b9a8f9c27354400160707751233b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sieveOddPrimesTo(bufferLimit: number) { | |
const lmti = (bufferLimit - 3) >> 1; | |
const sz = (lmti >> 3) + 1; | |
const cmpSts = new Uint8Array(sz); | |
for (let i = 0; ; ++i) { | |
const p = i + i + 3; | |
const sqri = (i << 1) * (i + 3) + 3; | |
if (sqri > lmti) { | |
break; | |
} | |
if ((cmpSts[i >> 3] & ((1 >>> 0) << (i & 7))) == 0 >>> 0) { | |
for (let c = sqri; c <= lmti; c += p) { | |
cmpSts[c >> 3] |= (1 >>> 0) << (c & 7); | |
} | |
} | |
} | |
let bi = -1; | |
return function () { | |
if (bi < 0) { | |
++bi; | |
} | |
while (bi <= lmti && (cmpSts[bi >> 3] & ((1 >>> 0) << (bi & 7))) != 0 >>> 0) { | |
++bi; | |
} | |
if (bi > lmti) { | |
// this can only happen when the number of pre-generated primes | |
// matches the number of requested primes exactly. | |
return null; | |
} | |
return (bi++ << 1) + 3; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment