-
Notifications
You must be signed in to change notification settings - Fork 5
/
factorial.metta
executable file
·301 lines (226 loc) · 7.69 KB
/
factorial.metta
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;
; 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) True)
(=
(factorial-standard $N $Result)
( (> $N 0)
(is $M
(- $N 1))
(factorial-standard $M $SubResult)
(is $Result
(* $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) True)
(=
(factorial-tail-basic3 $N $Accumulator $Result)
( (> $N 0)
(is $NewAccumulator
(* $N $Accumulator))
(is $M
(- $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)
(set-det))
(=
(factorial-between $N $Product $Result)
( (> $N 0)
(is $NewProduct
(* $N $Product))
(is $Next
(- $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)
(is $Z
(* $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)
(set-det))
(=
(factorial-tail-between $N $Product $Result)
( (> $N 0)
(is $NewProduct
(* $N $Product))
(is $Next
(- $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)
(set-det))
(=
(factorial-tail-cut $N $Accumulator $Result)
( (> $N 0)
(is $NewAccumulator
(* $N $Accumulator))
(is $M
(- $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)
(set-det))
(=
(factorial-acc-cut $N $Accumulator $Result)
( (> $N 0)
(is $NewAccumulator
(* $N $Accumulator))
(is $M
(- $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)
(is $NewAccumulator
(* $N $Accumulator))
(is $M
(- $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 MeTTa implementations.
!(table (/ factorial-tabled 2))
(=
(factorial-tabled 0 1)
(set-det))
(=
(factorial-tabled $N $F)
( (> $N 0)
(is $N1
(- $N 1))
(factorial-tabled $N1 $F1)
(is $F
(* $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)
(set-det))
(=
(factorial-memo $N $F)
( (> $N 0) (det-if-then-else (was-factorial-memo $N $F) True (, (is $N1 (- $N 1)) (factorial-memo $N1 $F1) (is $F (* $N $F1)) (add-atom &self (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)) True)
;
; Utility to run and time each Factorial implementation
(=
(time-factorials $N)
( (remove-all-atoms &self
(was_factorial_memo $_ $_))
(factorials $Factorials)
(member $Fib $Factorials)
(format '~N~n% =====================================================~n' Nil)
(=.. $Goal
(:: $Fib $N $_))
(statistics walltime
(Cons $Start $_))
(catch
(call $Goal) $E
(format '~N~nError in Goal: ~q ~q ~n'
(:: $Goal $E)))
(statistics walltime
(Cons $End $_))
(is $Time
(- $End $Start))
(format '~N~n~w(~w) took ~w ms~n'
(:: $Fib $N $Time))
(fail)))
; ; Clear any memoized results
(=
(time-factorials $_)
( (format '~N~n% =====================================================~n' Nil) (set-det)))
;
; Running the utility with an example, N=30.
;
; :- writeln(':- time_factorials(61111).').