%
% Project Euler, problem 2 ASP.
%
% 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.
% """
%
% This was created by Hakan Kjellerstrand, hakank@bonetmail.com
% See also http://www.hakank.org/answer_set_programming/
%
#const n=45. % max value of N
#const limit = 4000000.
number(1..n).
fib(0, 1).
fib(1, 1).
fib(N, X1 + X2) :- number(N), N > 1, fib(N - 1, X1), fib(N - 2, X2).
total(Sum) :- Sum = #sum [fib(N, Res) : Res < limit : Res #mod 2 == 0 = Res].
#hide.
#show total(Sum).