mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Refactor linearPrimeSieve.js
This commit is contained in:
parent
65037f32dd
commit
b43738839e
@ -3,22 +3,33 @@
|
||||
* @return {number[]}
|
||||
*/
|
||||
export default function linearPrimeSieve(maxNumber) {
|
||||
const primes =[];
|
||||
const leastPrimeFactor = new Array(maxNumber + 1).fill(0); // leastPrimeFactor[i] gives us the least prime factor of 'i'
|
||||
|
||||
for(let i=2; i<=maxNumber; i++ ){
|
||||
if(!leastPrimeFactor[i]){ // leastPrimeFactor[i] = 0 means 'i' itself is its least prime factor, i.e 'i' is prime
|
||||
leastPrimeFactor[i] = i;
|
||||
primes.push(i);
|
||||
}
|
||||
|
||||
/*
|
||||
* start setting leastPrimeFactor[] for numbers 'x', where x = p * i, p is x's least prime factor and p <= leastPrimeFactor[i]
|
||||
* x = p*i, this representation will be unique for any number, therefore leastPrimeFactor[x] will be set only once.
|
||||
*/
|
||||
for(let j=0; j<primes.length && primes[j]*i <=maxNumber && primes[j]<=leastPrimeFactor[i]; j++)
|
||||
leastPrimeFactor[primes[j]*i] = primes[j];
|
||||
}
|
||||
|
||||
return primes;
|
||||
const primes = [];
|
||||
// leastPrimeFactor[i] gives us the least prime factor of 'i'
|
||||
const leastPrimeFactor = new Array(maxNumber + 1).fill(0);
|
||||
|
||||
for (let i = 2; i <= maxNumber; i += 1) {
|
||||
if (!leastPrimeFactor[i]) {
|
||||
/* leastPrimeFactor[i] = 0 means 'i' itself is its least prime factor,
|
||||
* i.e 'i' is prime
|
||||
*/
|
||||
leastPrimeFactor[i] = i;
|
||||
primes.push(i);
|
||||
}
|
||||
|
||||
/*
|
||||
* start setting leastPrimeFactor[] for numbers 'x', where x = p * i, p is x's
|
||||
* least prime factor and p <= leastPrimeFactor[i]. x = p*i, this representation will
|
||||
* be unique for any number, therefore leastPrimeFactor[x] will be
|
||||
* set only once.
|
||||
*/
|
||||
|
||||
let j = 0;
|
||||
while (j < primes.length) {
|
||||
if ((primes[j] * i > maxNumber) || (primes[j] > leastPrimeFactor[i])) { break; }
|
||||
leastPrimeFactor[primes[j] * i] = primes[j];
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return primes;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user