From fe5ce90cdf1761e37bd8ffabd00a6890774ca9da Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:35 +0100 Subject: [PATCH] (euler) - #7 --- euler/src/7 - 10001st Prime.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 euler/src/7 - 10001st Prime.ts diff --git a/euler/src/7 - 10001st Prime.ts b/euler/src/7 - 10001st Prime.ts new file mode 100644 index 0000000..78b3b1c --- /dev/null +++ b/euler/src/7 - 10001st Prime.ts @@ -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));