% factorial(N,M) holds when M=Nx(N-1)x...x2x1.

% Naive version (non tail-recursive)
fact(1,1).
fact(N,M) :- N > 1, N1 is N-1, fact(N1,K), M is N*K.

% factorial with an accumulator
% call with accumulator initialized at 1: fact(N,1,M).
factA(N,M) :- factA(N,1,M). % Hide accumulator
factA(1,M,M).
factA(N,Acc,M) :- N > 0, Acc1 is Acc*N, N1 is (N-1), factA(N1,Acc1,M).
