-
Notifications
You must be signed in to change notification settings - Fork 15
/
n_factorial.pl
36 lines (29 loc) · 936 Bytes
/
n_factorial.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Relation between a natural number N and its factorial F.
CLP(Z) constraints allow for a concise declarative solution that
can be used in all directions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- use_module(clpz).
n_factorial(0, 1).
n_factorial(N, F) :-
N #> 0,
N1 #= N - 1,
F #= N * F1,
n_factorial(N1, F1).
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Examples:
?- n_factorial(47, F).
%@ F = 258623241511168180642964355153611979969197632389120000000000 ;
%@ false.
?- n_factorial(N, 1).
%@ N = 0 ;
%@ N = 1 ;
%@ false.
?- n_factorial(N, F).
%@ N = 0,
%@ F = 1 ;
%@ N = F, F = 1 ;
%@ N = F, F = 2 ;
%@ N = 3,
%@ F = 6 .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */