-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path002_even_fibonaccis.hs
30 lines (24 loc) · 1.11 KB
/
002_even_fibonaccis.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- 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.
fibonacci :: (Integral a) => a -> a
fibonacci 1 = 1
fibonacci 2 = 1
fibonacci n = fibonacci (n-1) + fibonacci (n-2)
listEvenFibonaccisToIndex :: (Integral a) => a -> [a]
listEvenFibonaccisToIndex n = [x | x <- (map fibonacci [1..n]), even x]
listEvenFibonaccisToValue :: (Integral a) => a -> [a]
listEvenFibonaccisToValue n = listEvenFibonaccisToValue' n [1] 2
listEvenFibonaccisToValue' :: (Integral a) => a -> [a] -> a -> [a]
listEvenFibonaccisToValue' n (x:xs) index
| x > n = [y | y <- xs, even y]
| otherwise = listEvenFibonaccisToValue' n ((fibonacci index):x:xs) (index + 1)
sumEvenFibonaccisToValue :: (Integral a) => a -> a
sumEvenFibonaccisToValue n = sum (listEvenFibonaccisToValue n)
main :: IO()
main = print $ sumEvenFibonaccisToValue 4000000