Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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.
- fib :: (Ord a, Integral a) => [a] -> [a]
- fib xs =
- if last xs < 4000000
- then fib (xs ++ [last xs + last (init xs)])
- else init xs
- main :: IO ()
- main = print $ sum $ filter even $ fib [0, 1]
- -- 4613732
- -- real 0m0,003s
- -- user 0m0,000s
- -- sys 0m0,003s
Add Comment
Please, Sign In to add comment