fib_count(Last, Current, Max, Sum, Sum) :- Current >= Max. fib_count(Last, Current, Max, Acc, Sum) :- 0 is Current rem 2, Next is Last + Current, NewAcc is Acc + Current, fib_count(Current, Next, Max, NewAcc, Sum). fib_count(Last, Current, Max, Acc, Sum) :- Next is Last + Current, fib_count(Current, Next, Max, Acc, Sum). e2(Result) :- fib_count(0, 1, 4000000, 0, Result).