diff options
| author | Mistivia <i@mistivia.com> | 2025-04-08 23:51:42 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-04-08 23:56:43 +0800 |
| commit | 211d23d948fdc42876b76c2724619de8329ff0c0 (patch) | |
| tree | 352ec0f4192f089976b70be020b588e7f467ecf6 /0007/main.hs | |
| parent | 05b6011d5b710f6ae93694f7af938def559f9ed2 (diff) | |
10 001st Prime
Diffstat (limited to '0007/main.hs')
| -rw-r--r-- | 0007/main.hs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/0007/main.hs b/0007/main.hs new file mode 100644 index 0000000..87486bd --- /dev/null +++ b/0007/main.hs @@ -0,0 +1,20 @@ +nthPrime :: Integer -> Integer +nthPrime targetCount = nextPrimeImpl 2 1 [] + where + nextPrimeImpl i primeCount primeList = + if isPrime then + if primeCount == targetCount then + i + else + nextPrimeImpl (i + 1) (primeCount + 1) (primeList ++ [i]) + else + nextPrimeImpl (i+1) primeCount primeList + where + isPrime = + isPrimeImpl primeList + where + isPrimeImpl [] = True + isPrimeImpl (x:xs) = i `mod` x /= 0 && isPrimeImpl xs + +main = do + putStrLn $ show $ nthPrime 10001 |
