Răsfoiți Sursa

10 001st Prime

Mistivia 5 zile în urmă
părinte
comite
211d23d948
2 a modificat fișierele cu 23 adăugiri și 0 ștergeri
  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