webvm/examples/nodejs/primes.js

32 lines
535 B
JavaScript
Raw Permalink Normal View History

2022-01-31 20:52:13 +00:00
(function () {
function isPrime(p) {
const upper = Math.sqrt(p);
for(let i = 2; i <= upper; i++) {
if (p % i === 0 ) {
return false;
}
}
return true;
}
// Return n-th prime
function prime(n) {
if (n < 1) {
throw Error("n too small: " + n);
}
let count = 0;
let result = 1;
while(count < n) {
result++;
if (isPrime(result)) {
count++;
}
}
return result;
}
console.log("your prime is ", prime(100000));
}());