Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit a8f4f4c

Browse files
committed
Added Sieve of Atkin algorithm and tests. Updated README.md(s).
1 parent 759d3be commit a8f4f4c

File tree

8 files changed

+140
-8
lines changed

8 files changed

+140
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Common algorithms implemented in JavaScript with [Mocha](https://mochajs.org/)/[
4040
* [Is Prime](https://github.com/sumtype/common-algorithms-js/blob/master/algorithms/es/isPrime.js)
4141
* [Power Set](https://github.com/sumtype/common-algorithms-js/blob/master/algorithms/es/powerSet.js)
4242
* [Prime Factors](https://github.com/sumtype/common-algorithms-js/blob/master/algorithms/es/primeFactors.js)
43+
* [Sieve of Atkin](https://github.com/sumtype/common-algorithms-js/blob/master/algorithms/es/sieveOfAtkin.js)
4344
* [Sieve of Eratosthenes](https://github.com/sumtype/common-algorithms-js/blob/master/algorithms/es/sieveOfEratosthenes.js)
4445

4546
### String

algorithms/es/sieveOfAtkin.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
A JavaScript module to perform the Sieve of Atkin algorithm, which determines prime numbers up to the input integer.
3+
*/
4+
5+
'use strict'
6+
7+
const sieveOfAtkin = (limit = 0) => {
8+
let primes = [2, 3]
9+
let sieve = new Array(limit)
10+
for (let x = 1, xSquared = x * x; xSquared < limit; x++) {
11+
xSquared = x * x
12+
for (let y = 1, ySquared = y * y; ySquared < limit; y++) {
13+
ySquared = y * y
14+
let n = (4 * xSquared) + ySquared
15+
if (n <= limit) {
16+
let mod12Check = n % 12
17+
if (mod12Check === 1 || mod12Check === 5) sieve[n] = !sieve[n]
18+
}
19+
let threeXSquared = (3 * xSquared)
20+
n = threeXSquared + ySquared
21+
if (n <= limit && n % 12 === 7) sieve[n] = !sieve[n]
22+
n = threeXSquared - ySquared
23+
if (x > y && n <= limit && n % 12 === 11) sieve[n] = !sieve[n]
24+
}
25+
}
26+
for (let r = 5; r * r < limit; r++) if (sieve[r]) for (let i = r * r; i < limit; i += r * r) sieve[i] = false
27+
for (let a = 5; a < limit; a++) if (sieve[a]) primes.push(a)
28+
return limit < 2 ? [] : limit === 2 ? [2] : limit === 3 ? [2, 3] : primes
29+
}
30+
31+
export default sieveOfAtkin

algorithms/es5/sieveOfAtkin.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(function (global, factory) {
2+
if (typeof define === 'function' && define.amd) {
3+
define(['exports'], factory)
4+
} else if (typeof exports !== 'undefined') {
5+
factory(exports)
6+
} else {
7+
var mod = {
8+
exports: {}
9+
}
10+
factory(mod.exports)
11+
global.sieveOfAtkin = mod.exports
12+
}
13+
})(this, function (exports) {
14+
'use strict'
15+
16+
Object.defineProperty(exports, '__esModule', {
17+
value: true
18+
})
19+
var sieveOfAtkin = function sieveOfAtkin () {
20+
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0
21+
22+
var primes = [2, 3]
23+
var sieve = new Array(limit)
24+
for (var x = 1, xSquared = x * x; xSquared < limit; x++) {
25+
xSquared = x * x
26+
for (var y = 1, ySquared = y * y; ySquared < limit; y++) {
27+
ySquared = y * y
28+
var n = 4 * xSquared + ySquared
29+
if (n <= limit) {
30+
var mod12Check = n % 12
31+
if (mod12Check === 1 || mod12Check === 5) sieve[n] = !sieve[n]
32+
}
33+
var threeXSquared = 3 * xSquared
34+
n = threeXSquared + ySquared
35+
if (n <= limit && n % 12 === 7) sieve[n] = !sieve[n]
36+
n = threeXSquared - ySquared
37+
if (x > y && n <= limit && n % 12 === 11) sieve[n] = !sieve[n]
38+
}
39+
}
40+
for (var r = 5; r * r < limit; r++) {
41+
if (sieve[r]) {
42+
for (var i = r * r; i < limit; i += r * r) {
43+
sieve[i] = false
44+
}
45+
}
46+
} for (var a = 5; a < limit; a++) {
47+
if (sieve[a]) primes.push(a)
48+
} return limit < 2 ? [] : limit === 2 ? [2] : limit === 3 ? [2, 3] : primes
49+
}
50+
51+
exports.default = sieveOfAtkin
52+
})

es.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import fibonacciNumber from './algorithms/es/fibonacciNumber'
3636
import isPrime from './algorithms/es/isPrime'
3737
import powerSet from './algorithms/es/powerSet'
3838
import primeFactors from './algorithms/es/primeFactors'
39+
import sieveOfAtkin from './algorithms/es/sieveOfAtkin'
3940
import sieveOfEratosthenes from './algorithms/es/sieveOfEratosthenes'
4041

4142
// String Algorithms
@@ -59,7 +60,7 @@ import sorensenDiceCoefficient from './algorithms/es/sorensenDiceCoefficient'
5960
const array = { binarySearch, bubbleSort, bucketSort, cocktailSort, countingSort, durstenfeldShuffle, gnomeSort, insertionSort, linearSearch, mergeSort, quickSort, radixSort, reservoirSampling, sattoloCycle, selectionSort, shellSort, sleepSort }
6061
const geometry = { bezierCurve, rayCasting }
6162
const graph = { breadthFirstSearch, depthFirstSearch }
62-
const math = { fibonacciNumber, isPrime, powerSet, primeFactors, sieveOfEratosthenes }
63+
const math = { fibonacciNumber, isPrime, powerSet, primeFactors, sieveOfAtkin, sieveOfEratosthenes }
6364
const string = { areAnagrams, arePalindromes, boyerMooreStringMatch, boyerMooreHorspoolStringMatch, bruteForceStringMatch, damerauLevenshteinDistance, hammingDistance, hasDuplicateCharacters, knuthMorrisPrattStringMatch, levenshteinDistance, longestCommonSubsequence, longestCommonSubstring, matchingDelimiters, rabinKarpStringMatch, sorensenDiceCoefficient }
6465

6566
// Algorithm Exports
@@ -68,6 +69,6 @@ export {
6869
array, binarySearch, bubbleSort, bucketSort, cocktailSort, countingSort, durstenfeldShuffle, gnomeSort, insertionSort, linearSearch, mergeSort, quickSort, radixSort, reservoirSampling, sattoloCycle, selectionSort, shellSort, sleepSort,
6970
geometry, bezierCurve, rayCasting,
7071
graph, breadthFirstSearch, depthFirstSearch,
71-
math, fibonacciNumber, isPrime, powerSet, primeFactors, sieveOfEratosthenes,
72+
math, fibonacciNumber, isPrime, powerSet, primeFactors, sieveOfAtkin, sieveOfEratosthenes,
7273
string, areAnagrams, arePalindromes, boyerMooreStringMatch, boyerMooreHorspoolStringMatch, bruteForceStringMatch, damerauLevenshteinDistance, hammingDistance, hasDuplicateCharacters, knuthMorrisPrattStringMatch, levenshteinDistance, longestCommonSubsequence, longestCommonSubstring, matchingDelimiters, rabinKarpStringMatch, sorensenDiceCoefficient
7374
}

0 commit comments

Comments
 (0)