-
Notifications
You must be signed in to change notification settings - Fork 33
/
primes.js
47 lines (40 loc) · 907 Bytes
/
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
function get_primes7(n) {
if (n < 2) { return []; }
if (n == 2) { return [2]; }
var s = [];
for (var i = 3; i < n + 1; i += 2) {
s.push(i);
}
var mroot = Math.floor(Math.sqrt(n));
var half = s.length;
var i = 0;
var m = 3;
while (m <= mroot) {
if (s[i]) {
var j = Math.floor((m*m-3)/2); // int div
s[j] = 0;
while (j < half) {
s[j] = 0;
j += m;
}
}
i = i + 1;
m = 2*i + 3;
}
// due to a bug in node.js 4.3, we need to declare and init on separate lines
// or else node.js performs about four times slower
var res = [];
res.push(2);
for (var x = 0; x < s.length; x++) {
if (s[x]) {
res.push(s[x]);
}
}
return res;
}
var startTime = Date.now();
var periodTime = parseInt(process.env.RUN_TIME, 10) * 1000
while ((Date.now() - startTime) < periodTime) {
var res = get_primes7(10000000);
console.log("Found " + res.length + " prime numbers.");
}