- Commit
- 7748222fd469bae042da905a01382047dee99923
- Parent
- cc4f39d56120c56106dd5125513c36e026de3389
- Author
- Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
- Date
Optimized the Prolog implementation.
An exercise on polyglossy: the same problem solved on multiple languages
Optimized the Prolog implementation.
1 file changed, 8 insertions, 6 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | script.pl | 14 | 8 | 6 |
diff --git a/script.pl b/script.pl @@ -3,13 +3,15 @@ % Let S: N -> N be the sum of the digits of a positive integer. % For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer. -sum_digits(X, X) :- X < 10. -sum_digits(X, Y) :- +sum_digits_acc(X, X, A) :- X < 10, A is 0, !. +sum_digits_acc(X, Y, A) :- X >= 10, X1 is div(X, 10), X2 is mod(X, 10), - sum_digits(X1, Y1), - Y is Y1 + X2. + sum_digits_acc(X1, A, _), + Y is A + X2. + +sum_digits(X, Y) :- sum_digits_acc(X, Y, _). test_pair(A, B) :- R is 0, @@ -21,13 +23,13 @@ test_pair(A, B) :- D is SC - SAB, R is mod(D, 9). -iter(A, 0) :- test_pair(A, 0). +iter(A, 0) :- test_pair(A, 0), !. iter(A, B) :- test_pair(A, B), C is B - 1, iter(A, C). -conjecture(0) :- test_pair(0, 0). +conjecture(0) :- test_pair(0, 0), !. conjecture(M) :- iter(M, M), N is M - 1,