Factorial of n is n!= 1 * 2* 3 * 4 *... * n or 0! = 1 n! = (n-1)! * n ?- fact(4,R). R = 24 fact(0,1). fact(N,R):- fact(N1,R1),N is N1 + 1,R is R1 * N.

Written with a collecting variable.?- fact(4,R). R = 24 fact(N,F):- fact1(N,1,F). fact1(0,F,F). fact1(N,X,F):- N is M + 1, Y is X * N, fact1(M,Y,F).