websieve/libsive.js

23 lines
420 B
JavaScript
Raw Permalink Normal View History

2024-10-21 22:14:38 +00:00
const isPrime = (num) => {
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
if (num % i === 0) return false;
}
return num > 1;
}
function sive(siveTo) {
let primes = [];
siveTo = Number(siveTo);
for (let i = 1; i < siveTo; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
if (isPrime(siveTo)) {
primes.push(siveTo);
}
return primes;
}