(euler) - #7
This commit is contained in:
parent
570a809c93
commit
fe5ce90cdf
1 changed files with 21 additions and 0 deletions
21
euler/src/7 - 10001st Prime.ts
Normal file
21
euler/src/7 - 10001st Prime.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
const isPrime = (number: number) => {
|
||||||
|
for (var i = 2; i < number; i++) {
|
||||||
|
if (number % i === 0) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const nthPrime = (n: number) => {
|
||||||
|
const primes: number[] = [];
|
||||||
|
let number = 2;
|
||||||
|
|
||||||
|
while (n > primes.length) {
|
||||||
|
if (isPrime(number)) primes.push(number);
|
||||||
|
number++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return primes[n - 1];
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(nthPrime(10001));
|
Loading…
Reference in a new issue