/*********************************************************
http://projecteuler.net/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, ...
By considering the terms in the Fibonacci sequence whose
values do not exceed four million, find the sum of the
even-valued terms.
*********************************************************/
main =>
S = 0,
I = 1,
F = fib(I),
while (F <= 4000000)
if (F mod 2 = 0) then
S:=S+F
end,
I:=I+1,
F:=fib(I)
end,
writef("Sum of the even-valued terms is %w%n",S).
table
fib(1) = 1.
fib(2) = 2.
fib(N)=fib(N-1)+fib(N-2).
/* here is another definition
fibs(Upto) = Fibs =>
L = [2,1],
F = L[1]+L[2],
while (F <= Upto)
L := [F|L],
F := L[1]+L[2]
end,
Fibs = L.
main =>
writeln(sum([X : X in fibs(4000000), even(X)])).
*/