/* Reversible factorial in Picat v3. Note: Max N that cp module can handle is 18! => 6402373705728000. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> member(N,1..20), println(n=N), n_factorial(N,F), println(f=F), % And get N back n_factorial(N2, F), println(n2=N2), nl, fail, nl. go => true. % Note that one don't have to use solve here. n_factorial(0, 1). n_factorial(N, F) :- N #> 0, N1 #= N - 1, F #= N * F1, n_factorial(N1, F1).