exp(X,0,1). exp(X,Y,Z) :- even(Y), R is Y/2, P is X*X, exp(P,R,Z). exp(X,Y,Z) :- T is Y-1, exp(X,T,Z1), Z is Z1*X. even(R) :- 0 is R mod 2.