From 211d23d948fdc42876b76c2724619de8329ff0c0 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Tue, 8 Apr 2025 23:51:42 +0800 Subject: 10 001st Prime --- .gitignore | 3 +++ 0007/main.hs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 .gitignore create mode 100644 0007/main.hs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c347055 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.hi +*.o +main 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 -- cgit v1.0