Skip to content

Instantly share code, notes, and snippets.

@rayyildiz
Last active September 5, 2016 07:13
Show Gist options
  • Save rayyildiz/065e2ee6c64b3ac215ad84b05f67c793 to your computer and use it in GitHub Desktop.
Save rayyildiz/065e2ee6c64b3ac215ad84b05f67c793 to your computer and use it in GitHub Desktop.
Even Fibonacci numbers (Project Euler Question 2)
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fibs = take 40 (1 : 1 : zipWith (+) fibs (tail fibs))
fibResult = sum [x | x <- fibs, x < 4000000, x `mod` 2 == 0 ]
@rayyildiz
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment