-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSieve.js
More file actions
87 lines (70 loc) · 1.7 KB
/
Sieve.js
File metadata and controls
87 lines (70 loc) · 1.7 KB
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
exports.atkinsSieve = function(limit){
var results = [2,3,5];
var sieve = new Array(limit+1);
for(var i = 0; i < sieve.length; i++){
sieve[i] = 0;
}
var factor = Math.floor(Math.sqrt(limit))+1;
var n,i,j;
for(i = 1; i < factor; i++){
for(j = 1; j < factor; j++){
n = 4 * (Math.pow(i,2)) + (Math.pow(j,2));
if((n <= limit) && ((n % 12 === 1) || (n % 12 === 5))){
sieve[n] = sieve[n] ^ 1;
}
n = 3 * (Math.pow(i,2)) + (Math.pow(j,2));
if((n <= limit) && (n % 12 === 7)){
sieve[n] = sieve[n] ^ 1;
}
if(i > j){
n = 3 * (Math.pow(i,2)) - (Math.pow(j,2));
if((n <= limit) && (n % 12 === 11)){
sieve[n] = sieve[n] ^ 1;
}
}
}
}
for(i = 5; i < factor; i++){
if(sieve[i] === 1){
for(j = (Math.pow(i,2)); j < limit; j += Math.pow(i,2)){
sieve[j] = 0;
}
}
}
// If the number is prime, push to the results array
for(i = 7; i< limit; i++){
if(sieve[i] ===1){
results.push(i);
}
}
return results;
}
exports.eratosthenesSieve = function(limit){
var sieve = new Array(limit);
var results = [];
// Initialize 1 to 0
sieve[0] = 0;
// Initialize 2 to 1
sieve[1] = 1;
// Initialize the starting array, ignoring multiples of 2
for(var i = 2; i < limit; i++){
((i+1) % 2) === 0 ? sieve[i] = 0: sieve[i] = 1;
}
// Iterate through numbers, skipping evens
for(var j = 2; j <= limit; j += 2){
// If the index is true
if(sieve[j]){
// Go to each multiple of this current number, they are not prime
for(var k = 2*(j+1); k <= limit; k +=(j+1)){
sieve[k-1] = 0;
}
}
}
// Gather the prime numbers
for(var l = 1; l <= limit; l++){
if(sieve[l] === 1){
results.push(l+1);
}
}
return results;
}