banovski

Project Euler, Problem #2, Haskell

Nov 27th, 2021 (edited)
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Each new term in the Fibonacci sequence is generated by adding the
  2. -- previous two terms. By starting with 1 and 2, the first 10 terms
  3. -- will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the
  4. -- terms in the Fibonacci sequence whose values do not exceed four
  5. -- million, find the sum of the even-valued terms.
  6.  
  7. fib :: (Ord a, Integral a) => [a] -> [a]
  8. fib xs =
  9.   if last xs < 4000000
  10.     then fib (xs ++ [last xs + last (init xs)])
  11.     else init xs
  12.  
  13. main :: IO ()
  14. main = print $ sum $ filter even $ fib [0, 1]
  15.  
  16. -- 4613732
  17.  
  18. -- real 0m0,003s
  19. -- user 0m0,000s
  20. -- sys  0m0,003s
  21.  
Add Comment
Please, Sign In to add comment