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.
;fibo (defn p2 [] (println (apply + (filter even? (take-while #(< % 4000001) (fibo)))) ) )
p2()-> p2(0,1,0). p2(A,_,Sum) when A >= 4000000 -> io:format("~w~n",[Sum]); p2(A,B,Sum)-> if A band 1 ==0 -> p2(B,A+B,Sum+A); true -> p2(B,A+B,Sum) end.
#Enumerable def p2 fib=[0,1] fib.push(fib[-1]+fib[-2]) while fib[-1]<4000000 puts fib[0...-1].select{|i|i%2==0}.sum end
def p2 { var fibo=List(1,0) while(fibo.head<=4000000)fibo=(fibo.head+fibo.tail.head)::fibo println(fibo.tail.filter(_%2==0).foldLeft(0)(_+_)) }