Переглянути джерело

[Haskell][2] Adding Solution

Vinicius Teshima 5 місяців тому
батько
коміт
5c0d0e9fdb
1 змінених файлів з 27 додано та 0 видалено
  1. 27 0
      haskell/src/0002.hs

+ 27 - 0
haskell/src/0002.hs

@@ -0,0 +1,27 @@
+{-
+Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
+1, 2, 3, 5, 8, 13, 21, 34, 55, 89
+By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
+-}
+
+--solution_inner :: Int -> Int
+--solution_inner x = go' x 0 where
+--  go' 4000000 y = y
+--  go' x y = go' (x+1) ((solution_is_div x) + y)
+
+is_even :: Int -> Int
+is_even x =
+  case (mod x 2) of
+    0 -> x
+    _ -> 0
+
+solution :: Int
+solution =
+  go' 1 2 0 where
+    limit = 4000000
+    go' x y sum
+      | x < limit && y < limit = go' y (x + y) $! ((is_even y) + sum)
+      | otherwise = sum
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)