-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfactorial.pl
executable file
·160 lines (141 loc) · 5.54 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
% Standard Recursive Approach
% Pros: Simple and easy to understand.
% Cons: Not efficient for large N as it is not tail recursive.
factorial_standard(0, 1).
factorial_standard(N, Result) :-
N > 0,
M is N - 1,
factorial_standard(M, SubResult),
Result is N * SubResult.
% Basic Tail Recursive Approach with Accumulator
% Pros: Tail recursive, efficient in stack frame usage for large N.
% Cons: Requires understanding of accumulators and tail recursion.
factorial_tail_basic2(N, Result) :-
factorial_tail_basic3(N, 1, Result).
factorial_tail_basic3(0, Accumulator, Accumulator).
factorial_tail_basic3(N, Accumulator, Result) :-
N > 0,
NewAccumulator is N * Accumulator,
M is N - 1,
factorial_tail_basic(M, NewAccumulator, Result).
% Factorial with between/3 Predicate
% Pros: Utilizes between/3 predicate, providing a clear range.
% Cons: Not tail recursive, can be inefficient for large N.
factorial_between(N, Result) :-
factorial_between(N, 1, Result).
factorial_between(0, Product, Product) :- !.
factorial_between(N, Product, Result) :-
N > 0,
NewProduct is N * Product,
Next is N - 1,
factorial_between(Next, NewProduct, Result).
% Factorial using findall/3 and product/2
% Pros: Utilizes findall/3 to generate a list, making it more adaptable.
% Cons: Generates a list of all numbers from 1 to N, can be memory-intensive for large N.
product(List, Product) :-
foldl(multiply, List, 1, Product).
multiply(X, Y, Z) :-
Z is X * Y.
factorial_findall(N, Result) :-
N >= 0,
findall(X, between(1, N, X), List),
product(List, Result).
% Using between/3 Predicate in Tail Recursive Manner
% Pros: Combines tail recursion with between/3 for clear range definition.
% Cons: Slightly more complex due to the combination of concepts.
factorial_tail_between(N, Result) :-
factorial_tail_between(N, 1, Result).
factorial_tail_between(0, Product, Product) :- !.
factorial_tail_between(N, Product, Result) :-
N > 0,
NewProduct is N * Product,
Next is N - 1,
factorial_tail_between(Next, NewProduct, Result).
% Tail Recursion with Explicit Cut
% Pros: Uses explicit cut to avoid unnecessary backtracking, optimizing performance.
% Cons: The use of cut (!) requires caution as it can affect the logic if misplaced.
factorial_tail_cut(N, Result) :-
factorial_tail_cut(N, 1, Result).
factorial_tail_cut(0, Accumulator, Accumulator) :- !.
factorial_tail_cut(N, Accumulator, Result) :-
N > 0,
NewAccumulator is N * Accumulator,
M is N - 1,
factorial_tail_cut(M, NewAccumulator, Result).
% Accumulator Version with Explicit Cut
% Pros: Efficient for large N due to tail recursion and avoids unnecessary backtracking due to cut.
% Cons: Requires understanding of both accumulators and the effect of cut on logic.
factorial_acc_cut(N, Result) :-
factorial_acc_cut(N, 1, Result).
factorial_acc_cut(0, Accumulator, Accumulator) :- !.
factorial_acc_cut(N, Accumulator, Result) :-
N > 0,
NewAccumulator is N * Accumulator,
M is N - 1,
factorial_acc_cut(M, NewAccumulator, Result).
% Accumulator Version with Guard Clauses
% Pros: Uses guard clauses for clear distinction between base and recursive cases. Efficient for large N.
% Cons: Slightly more verbose due to the explicit guard clauses.
factorial_acc_guard(N, Result) :-
factorial_acc_guard(N, 1, Result).
factorial_acc_guard(N, Accumulator, Accumulator) :-
N =< 0.
factorial_acc_guard(N, Accumulator, Result) :-
N > 0,
NewAccumulator is N * Accumulator,
M is N - 1,
factorial_acc_guard(M, NewAccumulator, Result).
% Tabling Method
% Summary: Uses tabling to store intermediate results, avoiding redundant calculations and improving efficiency.
% Pros: Efficient even for large N due to no recalculations; polynomial time complexity.
% Cons: Uses additional memory to store the intermediate results; support for tabling is not available in all Prolog implementations.
:- table factorial_tabled/2.
factorial_tabled(0, 1) :- !.
factorial_tabled(N, F) :-
N > 0,
N1 is N - 1,
factorial_tabled(N1, F1),
F is N * F1.
% Memoization (Dynamic Programming) Method
% Summary: Uses memoization to store previously calculated factorials, improving efficiency.
% Pros: Efficient even for large N due to no recalculations.
% Cons: Uses additional memory to store the previously calculated factorials.
:- dynamic was_factorial_memo/2.
factorial_memo(0, 1) :- !.
factorial_memo(N, F) :-
N > 0,
( was_factorial_memo(N, F) -> true ;
N1 is N - 1,
factorial_memo(N1, F1),
F is N * F1,
assertz(was_factorial_memo(N, F)) ).
% List of all the factorial implementations
factorials([
factorial_standard,
factorial_between,
factorial_findall,
factorial_tail_basic2,
factorial_tail_between,
factorial_tabled,
factorial_memo,
factorial_tail_cut,
factorial_acc_cut,
factorial_acc_guard
]).
% Utility to run and time each Factorial implementation
time_factorials(N) :-
retractall(was_factorial_memo(_,_)), % Clear any memoized results
factorials(Factorials),
member(Fib, Factorials),
format('~N~n% =====================================================~n',[]),
Goal=..[Fib,N,_],
statistics(walltime, [Start|_]),
catch(call(Goal),E,format('~N~nError in Goal: ~q ~q ~n',[Goal,E])),
statistics(walltime, [End|_]),
Time is End - Start,
format('~N~n~w(~w) took \t~w ms~n', [Fib, N, Time]),
fail.
time_factorials(_):-
format('~N~n% =====================================================~n',[]),!.
% Running the utility with an example, N=30.
%:- writeln(':- time_factorials(61111).').