2.24.2011

Euler 002

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.
My solution, in Haskell:


f=(scanl(+)0(1:f))
s=sum$filter even$takeWhile(<4000000)f
Can't get rid of that space, sadly! 57 characters. And newlines count!

Your shot, Neil.

1 comment:

  1. 57. Ignore the dirty 4*10e5 trick. (Ruby)

    x,y,z=1,2,0;(x%2==0?z+=x:0;y+=x;x=y-x;)while x<4*10e5;p z

    ReplyDelete