domenica 5 settembre 2010

Problem 2

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, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

solution = sum $ filter even $ fibsLowerThan 4000000

fibsLowerThan upperBound = fibs 0 upperBound 

fibs :: Integral a ⇒ a → a → [a]
fibs n upperBound
| fib n > upperBound = []
| otherwise = fib n : fibs (n + 1) upperBound

fib 0 = 0
fib 1 = 1
fib n = sum [fib $ n - 1, fib $ n - 2]




Too much time to write it right... wtf...

Nessun commento:

Posta un commento