Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,31 @@
|
|||
"use strict";
|
||||
|
||||
function countPrimesTo(lmt) {
|
||||
if (lmt < 3) { if (lmt < 2) return 0; else return 1; }
|
||||
const sqrtlmt = Math.sqrt(lmt) >>> 0;
|
||||
const oprms = function() {
|
||||
const mxndx = (sqrtlmt - 3) >>> 1;
|
||||
const arr = new Float64Array(mxndx + 1);
|
||||
for (let i = 0 >>> 0; i <= mxndx; ++i) arr[i] = (i + i + 3) >>> 0;
|
||||
let bp = 3 >>> 0;
|
||||
while (true) {
|
||||
let i = (bp - 3) >>> 1; let sqri = ((i + i) * (i + 3) + 3) >>> 0;
|
||||
if (sqri > mxndx) break;
|
||||
if (arr[i] != 0) for (; sqri <= mxndx; sqri += bp) arr[sqri] = 0;
|
||||
bp += 2;
|
||||
}
|
||||
return arr.filter(v => v != 0);
|
||||
}();
|
||||
function phi(x, a) {
|
||||
if (a <= 0) return x - Math.trunc(x / 2);
|
||||
const na = (a - 1) >>> 0; const p = oprms[na];
|
||||
if (x <= p) return 1;
|
||||
return phi(x, na) - phi(Math.trunc(x / p), na);
|
||||
}
|
||||
return phi(lmt, oprms.length) + oprms.length;
|
||||
}
|
||||
|
||||
const start = Date.now();
|
||||
for (let i = 0; i <= 9; ++i) console.log(`π(10**${i}) =`, countPrimesTo(10**i));
|
||||
const elpsd = Date.now() - start;
|
||||
console.log("This took", elpsd, "milliseconds.")
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
const TinyPhiPrimes = [ 2, 3, 5, 7, 11, 13 ];
|
||||
const TinyPhiOddDegree = TinyPhiPrimes.length - 1;
|
||||
const TinyPhiOddCirc = TinyPhiPrimes.reduce((acc, p) => acc * p) / 2;
|
||||
const TinyPhiTot = TinyPhiPrimes.reduce((acc, p) => acc * (p - 1), 1)
|
||||
const TinyPhiLUT = function() {
|
||||
const arr = new Uint16Array(TinyPhiOddCirc); arr.fill(1);
|
||||
for (const p of TinyPhiPrimes) {
|
||||
if (p <= 2) continue; arr[p >> 1] = 0;
|
||||
for (let c = (p * p) >> 1; c < TinyPhiOddCirc; c += p) arr[c] = 0 >>> 0; }
|
||||
for (let i = 0 | 0, acc = 0 | 0; i < TinyPhiOddCirc; ++i) {
|
||||
acc += arr[i]; arr[i] = acc; }
|
||||
return arr; }();
|
||||
function tinyPhi(x) {
|
||||
const ndx = Math.trunc(( x - 1) / 2);
|
||||
const numtots = Math.trunc(ndx / TinyPhiOddCirc);
|
||||
const rem = (ndx - numtots * TinyPhiOddCirc) >>> 0;
|
||||
return numtots * TinyPhiTot + TinyPhiLUT[rem];
|
||||
}
|
||||
|
||||
function countPrimesTo(lmt) {
|
||||
if (lmt < 169) {
|
||||
if (lmt < 3) { if (lmt < 2) return 0; else return 1; }
|
||||
// adjust for the missing "degree" base primes
|
||||
if (lmt <= 13) return ((lmt - 1) >>> 1) + (lmt < 9 ? 1 : 0);
|
||||
return 5 + TinyPhiLUT[(lmt - 1) >>> 1];
|
||||
}
|
||||
const sqrtlmt = Math.sqrt(lmt) >>> 0;
|
||||
const oprms = function() {
|
||||
const mxndx = (sqrtlmt - 3) >>> 1;
|
||||
const arr = new Float64Array(mxndx + 1);
|
||||
for (let i = 0 >>> 0; i <= mxndx; ++i) arr[i] = (i + i + 3) >>> 0;
|
||||
let bp = 3 >>> 0;
|
||||
while (true) {
|
||||
let i = (bp - 3) >>> 1; let sqri = ((i + i) * (i + 3) + 3) >>> 0;
|
||||
if (sqri > mxndx) break;
|
||||
if (arr[i] != 0) for (; sqri <= mxndx; sqri += bp) arr[sqri] = 0;
|
||||
bp += 2;
|
||||
}
|
||||
return arr.filter(v => v != 0); }();
|
||||
function lvl(pilmt, m) {
|
||||
let ans = 0;
|
||||
for (let pi = TinyPhiOddDegree; pi < pilmt; ++pi) {
|
||||
const p = oprms[pi]; const nm = m * p;
|
||||
if (lmt <= nm * p) return ans + pilmt - pi;
|
||||
ans += tinyPhi(Math.trunc(lmt / nm));
|
||||
if (pi > TinyPhiOddDegree) ans -= lvl(pi, nm);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
return tinyPhi(lmt) - lvl(oprms.length, 1) + oprms.length;
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
const masks = new Uint8Array(8); // faster than bit twiddling!
|
||||
for (let i = 0; i < 8; ++i) masks[i] = (1 << i) >>> 0;
|
||||
|
||||
function countPrimesTo(lmt) {
|
||||
if (lmt < 3) { if (lmt < 2) return 0; else return 1; }
|
||||
function half(x) { return (x - 1) >>> 1; }
|
||||
function divide(nm, d) { return (nm / d) >>> 0; }
|
||||
const sqrtlmt = Math.trunc(Math.sqrt(lmt));
|
||||
const mxndx = (sqrtlmt - 1) >>> 1; const cbsz = (mxndx + 8) >>> 3;
|
||||
const cullbuf = new Uint8Array(cbsz);
|
||||
const smalls = new Uint32Array(mxndx + 1);
|
||||
for (let i = 0; i <= mxndx; ++i) smalls[i] = i >>> 0;
|
||||
const roughs = new Uint32Array(mxndx + 1);
|
||||
for (let i = 0; i <= mxndx; ++i) roughs[i] = (i + i + 1) >>> 0;
|
||||
const larges = new Float64Array(mxndx + 1);
|
||||
for (let i = 0; i <= mxndx; ++i) larges[i] =
|
||||
Math.trunc((Math.trunc(lmt / (i + i + 1)) - 1) / 2);
|
||||
|
||||
// partial sieve loop, adjusting larges/smalls, compressing larges/roughs...
|
||||
let nobps = 0 >>> 0; let rilmt = mxndx; let bp = 3 >>> 0;
|
||||
while (true) { // break when square root is reached
|
||||
const i = bp >>> 1; let sqri = ((i + i) * (i + 1)) >>> 0;
|
||||
if (sqri > mxndx) break; // partial sieving pass if bp is prime:
|
||||
if ((cullbuf[i >> 3] & masks[i & 7]) == 0) {
|
||||
cullbuf[i >> 3] |= masks[i & 7]; // cull bp!
|
||||
for (; sqri <= mxndx; sqri += bp)
|
||||
cullbuf[sqri >> 3] |= masks[sqri & 7]; // cull bp mults!
|
||||
|
||||
// now adjust `larges` for latest partial sieve pass...
|
||||
var ori = 0 // compress input rough index to output one
|
||||
for (let iri = 0; iri <= rilmt; ++iri) {
|
||||
const r = roughs[iri]; const rci = r >>> 1; // skip roughs just culled!
|
||||
if ((cullbuf[rci >> 3] & masks[rci & 7]) != 0) continue;
|
||||
const d = bp * r;
|
||||
larges[ori] = larges[iri] -
|
||||
( (d <= sqrtlmt) ? larges[smalls[d >>> 1] - nobps]
|
||||
: smalls[half(divide(lmt, d))] ) +
|
||||
nobps; // base primes count over subtracted!
|
||||
roughs[ori++] = r;
|
||||
}
|
||||
|
||||
let si = mxndx // and adjust `smalls` for latest partial sieve pass...
|
||||
for (let bpm = (sqrtlmt / bp - 1) | 1; bpm >= bp; bpm -= 2) {
|
||||
const c = smalls[bpm >>> 1] - nobps;
|
||||
const ei = ((bpm * bp) >>> 1);
|
||||
while (si >= ei) smalls[si--] -= c;
|
||||
}
|
||||
|
||||
nobps++; rilmt = ori - 1;
|
||||
}
|
||||
bp += 2;
|
||||
}
|
||||
|
||||
// combine results to here; correcting for over subtraction in combining...
|
||||
let ans = larges[0]; for (let i = 1; i <= rilmt; ++i) ans -= larges[i];
|
||||
ans += Math.trunc((rilmt + 1 + 2 * (nobps - 1)) * rilmt / 2);
|
||||
|
||||
// add final adjustment for pairs of current roughs to cube root of range...
|
||||
let ri = 0
|
||||
while (true) { // break when reaches cube root of counting range...
|
||||
const p = roughs[++ri]; const q = Math.trunc(lmt / p);
|
||||
const ei = smalls[half(divide(q, p))] - nobps;
|
||||
if (ei <= ri) break; // break here when no more pairs!
|
||||
for (let ori = ri + 1; ori <= ei; ++ori)
|
||||
ans += smalls[half(divide(q, roughs[ori]))];
|
||||
ans -= (ei - ri) * (nobps + ri - 1);
|
||||
}
|
||||
|
||||
return ans + 1; // add one for only even prime of two!
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue