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,