:- dynamic lookup/2. fak(0, 1). fak(N, X) :- lookup(N, X), !. fak(N, X) :- N > 0, N2 is N - 1, fak(N2, X2), X is N * X2, assert(lookup(N, X)).