my-first-prime-sive/sive.js
2024-10-21 21:47:03 +00:00

31 lines
No EOL
663 B
JavaScript

const readline = require("readline");
const { stdin: input, stdout: output } = require('node:process');
let primes = []
const isPrime = num => {
for(let i = 2, s = Math.sqrt(num); i <= s; i++) {
if(num % i === 0) return false;
}
return num > 1;
} //the only part taken from stackoverflow lmao...
const rl = readline.createInterface({ input, output });
rl.question('Sive to: ', (siveTo) => {
siveTo = Number(siveTo)
for (var i = 1; i < siveTo; i++) {
if (isPrime(i)) {
primes.push(i)
}
}
if (isPrime(siveTo)) {
primes.push(siveTo)
}
console.table(primes)
rl.close()
})