瀏覽代碼

10 001st Prime

Mistivia 1 周之前
父節點
當前提交
211d23d948
共有 2 個文件被更改,包括 23 次插入0 次删除
  1. 3 0
      .gitignore
  2. 20 0
      0007/main.hs

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+*.hi
+*.o
+main

+ 20 - 0
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