Selaa lähdekoodia

[Haskell][7] Adding Solution

Vinicius Teshima 5 kuukautta sitten
vanhempi
sitoutus
c4aaaaa563
1 muutettua tiedostoa jossa 27 lisäystä ja 0 poistoa
  1. 27 0
      haskell/src/0007.hs

+ 27 - 0
haskell/src/0007.hs

@@ -0,0 +1,27 @@
+{-
+By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
+What is the 10001st prime number?
+-}
+
+is_prime :: Int -> Bool
+is_prime x = go' x 2
+  where
+    go' :: Int -> Int -> Bool
+    go' x y
+      | x == y    = True
+      | x == 1    = True
+      | y == 2    = if (mod x 2) == 0 then False else go' x 3
+      | otherwise = if (mod x y) == 0 then False else go' x (y+2)
+
+solution :: Int
+solution = go' 3 2
+  where
+    go' :: Int -> Int -> Int
+    go' x count = if (is_prime x)
+                  then if count > 10000
+                       then x
+                       else go' (x + 2) (count + 1)
+                  else go' (x + 2) count
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)