my-first-prime-sive/sive.js

33 lines
665 B
JavaScript
Raw Normal View History

2024-10-21 21:21:04 +00:00
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)
}
}
2024-10-21 21:23:48 +00:00
if (isPrime(siveTo)) {
primes.push(siveTo)
}
2024-10-21 21:21:04 +00:00
console.table(primes)
rl.close()
})