programming-examples/js/Math/Get all prime numbers from 0 to a specified number .js

20 lines
525 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function primeFactorsTo(max)
{
var store = [], i, j, primes = [];
for (i = 2; i <= max; ++i)
{
if (!store [i])
{
primes.push(i);
for (j = i << 1; j <= max; j += i)
{
store[j] = true;
}
}
}
return primes;
}
console.log(primeFactorsTo(5));
console.log(primeFactorsTo(15));