-
Notifications
You must be signed in to change notification settings - Fork 256
/
primes.js
155 lines (134 loc) · 3.42 KB
/
primes.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
const net = require('net');
const UPPER_BOUND = 5000000;
const PREFIX = 32338;
class Node {
constructor() {
this.children = new Object();
this.terminal = false;
}
}
const toInt = function(n) {
return n >>> 0;
}
class Sieve {
constructor(limit) {
this.limit = limit;
this.prime = new Int8Array(limit + 1);
}
toList() {
const result = [2, 3];
for (let p = 5; p <= this.limit; ++p) {
if (this.prime[p]) {
result.push(p);
}
}
return result;
}
omitSquares() {
for (let r = 5; r * r < this.limit; ++r) {
if (this.prime[r]) {
for (let i = r * r; i < this.limit; i += r * r) {
this.prime[i] = 0;
}
}
}
return this;
}
step1(x, y) {
const n = (4 * x * x) + (y * y);
if (n <= this.limit && (n % 12 == 1 || n % 12 == 5)) {
this.prime[n] = !this.prime[n];
}
}
step2(x, y) {
const n = (3 * x * x) + (y * y);
if (n <= this.limit && n % 12 == 7) {
this.prime[n] = !this.prime[n];
}
}
step3(x, y) {
const n = (3 * x * x) - (y * y);
if (x > y && n <= this.limit && n % 12 == 11) {
this.prime[n] = !this.prime[n];
}
}
loopY(x) {
for (let y = 1; y * y < this.limit; ++y) {
this.step1(x, y);
this.step2(x, y);
this.step3(x, y);
}
}
loopX() {
for (let x = 1; x * x < this.limit; ++x) {
this.loopY(x);
}
}
calc() {
this.loopX();
return this.omitSquares();
}
}
function generateTrie(l) {
const root = new Node()
for (const el of l) {
let head = root;
for (const ch of el.toString()) {
if (!Object.hasOwn(head.children, ch)) {
head.children[ch] = new Node();
}
head = head.children[ch];
}
head.terminal = true;
}
return root;
}
function find(upperBound, prefix) {
const primes = new Sieve(upperBound).calc();
const strPrefix = prefix.toString();
let head = generateTrie(primes.toList());
for (const ch of strPrefix) {
head = head.children[ch];
if (typeof head === 'undefined') {
return null;
}
}
let [queue, result] = [[[head, strPrefix]], []]
while (queue.length > 0) {
const [top, prefix] = queue.pop();
if (top.terminal) {
result.push(toInt(prefix));
}
for (const [ch, v] of Object.entries(top.children)) {
queue.splice(0, 0, [v, prefix + ch]);
}
}
result.sort();
return result;
}
function notify(msg) {
return new Promise(resolve => {
const client = net.connect(9001, 'localhost', () => {
client.end(msg, 'utf8', () => {
client.destroy();
resolve();
});
}).on('error', resolve);
});
}
function verify() {
const left = [2, 23, 29];
const right = find(100, 2);
if (left.toString() != right.toString()) {
console.error(`${left} != ${right}`)
process.exit(1);
}
}
(async function() {
verify();
await notify(`Node.js\t${process.pid}`);
const results = find(UPPER_BOUND, PREFIX);
await notify('stop');
console.log(results);
})();