-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenb.mjs
92 lines (84 loc) · 1.77 KB
/
genb.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import fs from 'fs';
const primes = [];
function isPrime(n, k) {
n = BigInt(n);
if (n <= 1n) {
return false;
}
for (const p of [2n, 3n, 5n, 7n]) {
if (p == n) {
return true;
} else if (n % p == 0n) {
return false;
}
if (p > n) {
break;
}
}
let d = n - 1n;
while (d % 2n == 0n) {
d /= 2n;
}
for (let i = 0; i < k; i++) {
if (!miller(d, n)) {
return false;
}
}
return true;
function miller(d, n) {
let a = 2n + (randin(0n, n - 2n) % (n - 4n));
let x = pow(a, d, n);
if (x == 1n || x == n - 1n) {
return true;
}
while (d != n - 1n) {
x = (x * x) % n;
d *= 2n;
if (x == 1n) {
return false;
}
if (x == n - 1n) {
return true;
}
}
return false;
function pow(x, y, p) {
let r = 1n;
x = x % p;
while (y > 0n) {
if (y & 1n) {
r = (r * x) % p;
}
y = y >> 1n;
x = (x * x) % p;
}
return r;
}
function randin(l, h) {
const d = h - l;
const dl = d.toString().length;
let m = '';
while (m.length < dl) {
m += Math.random().toString().split('.')[1];
}
m = m.slice(0, dl);
const rd = (d * BigInt(m)) / BigInt('1' + '0'.repeat(dl));
return l + rd;
}
}
}
function getPrimes(min, max) {
const a = [];
f: for (let i = min; i <= max; i++) {
if (!isPrime(i, (i + '').length)) continue f;
primes.push(i);
a.push(i);
}
return a;
}
for (let i = 0; i < 100; i++) {
const t = new Date().getTime();
const a = getPrimes(Math.max(2, i * 100000), (i + 1) * 100000);
console.log(i, a.length, primes.length, new Date().getTime() - t + 'ms');
}
fs.writeFileSync('primesb.json', JSON.stringify(primes));