a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

knowledge-base.pl (738B)

 1 % The following script is a simple test for the following conjecture:
 2 
 3 % Let S: N -> N be the sum of the digits of a positive integer.
 4 % For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.
 5 
 6 sum_digits(X, Y) :- sum_digits_acc(X, 0, Y).
 7 
 8 sum_digits_acc(0, Acc, Acc) :- !.
 9 sum_digits_acc(X, Acc, Y) :-
10     Q is X div 10,
11     R is X mod 10,
12     Acc1 is Acc + R,            % Accumulate value
13     sum_digits_acc(Q, Acc1, Y). % Propagate Y (result) from recursion back up
14 
15 test_pair(A, B) :-
16     sum_digits(A, X),
17     sum_digits(B, Y),
18     sum_digits(A + B, Z),
19     D   = Z - X - Y,
20     0 =:= D mod 9.
21 
22 conjecture(Max) :-
23     forall(between(0, Max, A), forall(between(0, A, B), test_pair(A, B))).
24