Kaynağa Gözat

[Haskell][10] Adding Solution

Vinicius Teshima 5 ay önce
ebeveyn
işleme
dce22bcf35
1 değiştirilmiş dosya ile 21 ekleme ve 0 silme
  1. 21 0
      haskell/src/0010.hs

+ 21 - 0
haskell/src/0010.hs

@@ -0,0 +1,21 @@
+{-
+The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
+Find the sum of all the primes below two million.
+-}
+
+is_prime :: Int -> Bool
+is_prime x = go' x 2
+  where
+    go' :: Int -> Int -> Bool
+    go' 1 y = True
+    go' 2 y = True
+    go' x y
+      | x == y    = True
+      | y == 2    = if (mod x y) == 0 then False else go' x 3
+      | otherwise = if (mod x y) == 0 then False else go' x (y+2)
+
+solution :: Int
+solution = sum (filter is_prime [2..2000000])
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)