Skip to content

Commit 15b5bd8

Browse files
authored
Merge pull request mgechev#140 from brunohadlich/sieveOfAtkins
Added Sieve Of Atkins implementation.
2 parents 4040286 + 5c06a43 commit 15b5bd8

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/primes/sieve-of-atkins.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
(function (exports) {
2+
'use strict';
3+
4+
/**
5+
* Sieve of Atkins.
6+
*
7+
* Modern algorithm for finding all prime numbers up to a specified integer.
8+
*
9+
* Returns list of primes up to specified limit.
10+
*
11+
* For example, for limit 10 it should return following list of primes:
12+
* [2, 3, 5, 7].
13+
*
14+
* @module primes/sieve-of-atkins
15+
* @param {Number} limit - Algorithm will returns list of primes up to
16+
* specified limit.
17+
* @returns {Array} Will return list with all prime numbers up to provided.
18+
* limit.
19+
*
20+
* @example
21+
* var sieveOfAtkins =
22+
* require('path-to-algorithms/src/sieve-of-atkins').sieveOfAtkins;
23+
*
24+
* console.log(sieveOfAtkins(12)); // [2, 3, 5, 7, 11]
25+
*/
26+
exports.sieveOfAtkins = function (limit) {
27+
if (limit <= 1) {
28+
return [];
29+
}
30+
31+
const sieve = Array(limit + 1);
32+
33+
const testingLimit = Math.ceil(Math.sqrt(limit));
34+
35+
var i;
36+
var j;
37+
var n;
38+
39+
for (i = 1; i < testingLimit; i += 1) {
40+
var ii = i * i;
41+
for (j = 1; j < testingLimit; j += 1) {
42+
var jj = j * j;
43+
if (ii + jj >= limit) {
44+
break;
45+
}
46+
47+
n = 4 * ii + jj;
48+
if (n <= limit && (n % 12 === 1 || n % 12 === 5)) {
49+
sieve[n] = !sieve[n];
50+
}
51+
52+
n = 3 * ii + jj;
53+
if (n <= limit && (n % 12 === 7)) {
54+
sieve[n] = !sieve[n];
55+
}
56+
57+
n = 3 * ii - jj;
58+
if (i > j && n <= limit && (n % 12 === 11)) {
59+
sieve[n] = !sieve[n];
60+
}
61+
}
62+
}
63+
64+
for (n = 5; n <= testingLimit; n += 1) {
65+
if (sieve[n]) {
66+
j = n * n;
67+
for (i = j; i <= limit; i += j) {
68+
sieve[i] = false;
69+
}
70+
}
71+
}
72+
73+
const primes = [2];
74+
75+
if (limit > 2) {
76+
primes.push(3);
77+
}
78+
79+
sieve.forEach(function (value, key) {
80+
if (value) {
81+
this.push(key);
82+
}
83+
}, primes);
84+
85+
return primes;
86+
}
87+
}(typeof exports === 'undefined' ? window : exports));

test/primes/sieve-of-atkins.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var sieveOfAtkins =
2+
require('../../src/primes/sieve-of-atkins').sieveOfAtkins;
3+
4+
describe('Sieve Of Atkins', function () {
5+
'use strict';
6+
7+
it('should give the right sequence of primes for limit 12', function () {
8+
expect(sieveOfAtkins(12).toString())
9+
.toEqual([2, 3, 5, 7, 11].toString());
10+
});
11+
12+
it('should give the empty list for limit less or equal 1', function () {
13+
expect(sieveOfAtkins(-12).toString()).toEqual([].toString());
14+
expect(sieveOfAtkins(0).toString()).toEqual([].toString());
15+
expect(sieveOfAtkins(1).toString()).toEqual([].toString());
16+
});
17+
18+
it('sum of prime numbers up to 2000000 limit should be 142913828922', function () {
19+
var sieve = sieveOfAtkins(2000000);
20+
var sumOfPrimes = sieve.reduce(function (previousValue, currentValue) {
21+
return previousValue + currentValue;
22+
});
23+
24+
expect(sumOfPrimes).toEqual(142913828922);
25+
});
26+
});

0 commit comments

Comments
 (0)