I recently started learning Prolog and got stuck on this question...
How do I change this rule below to give me the value of "A*1 + A*2 + A*3 + ... + A*N"?
sum(1,A,1).
sum(N,A,Res) :-
N > 1,
Nminus1 is N-1,
sum(Nminus1,A,Res2),
Res is Res2 + N*A.
The result always appears to be off by one, as shown in the following query:
?- sum(4,2,Res).
Res = 19 ; % NO! expected: `Res = 20`
false.
I can't figure it out. Thank You in advance!