Skip to content

Commit

Permalink
Added the sieve of Eratosthenes
Browse files Browse the repository at this point in the history
  • Loading branch information
aasierra committed Oct 8, 2015
1 parent 28d623f commit 8e41335
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/GCD.js
@@ -1,7 +1,7 @@
/**
This method find the greatest common denominator of two objects using the modules axiom.
@param {Number} The higher number of the two parameters.
@param {Number} The lower number of the two parameters.
@param {Number} The higher number a, of the two parameters where a > 0
@param {Number} The lower number b, of the two parameters where b > 0
*/
Math.gcd = function(a, b) {
return b === 0 ? a : Math.gcd(b, a % b);
Expand Down
28 changes: 28 additions & 0 deletions src/soe.js
@@ -0,0 +1,28 @@
/**
This function is known as the sieve of Eratosthenese https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
This function will find and return an Array of the prime numbers below a certain integer number n where n > 2.
@param {Number} An integer number greater then 2
*/
Math.soe = function (n) {
var possibilities = {};
for (var i = 2; i <= n; i++) {//First we create the array of possible numbers
possibilities[i] = i;
}
//We only go here until i * i < n since earlier passes are eliminating based on this criteria. We can assume that i * i must be less then n to even begin to be compared.
for (var i = 2; i <= Math.floor(Math.sqrt(n)); i++) {
if (possibilities[i] !== 0) {
var count = i * i;
while (count <= n) {
possibilities[count] = 0;
count += i;
}
}
}
var primes = [];
for (var i = 2; i < n; i++) {
if (possibilities[i] != 0) {
primes.push(possibilities[i]);
}
}
return primes;
}

0 comments on commit 8e41335

Please sign in to comment.