Cryptoarithmetic puzzle 2


A program for cryptoarithmetic puzzles.

Source: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7.


Program source code:


%  Figure 7.2  A program for cryptoarithmetic puzzles.
%    Solving cryptarithmetic puzzles
sum(N1, N2, N)  :-                    % Numbers represented as lists of digits
  sum1( N1, N2, N, 
	0, 0,                         % Carries from right and to left both 0
        [0,1,2,3,4,5,6,7,8,9], _).    % All digits available
sum1( [], [], [], C, C, Digits, Digits).
sum1( [D1|N1], [D2|N2], [D|N], C1, C, Digs1, Digs)  :-
  sum1( N1, N2, N, C1, C2, Digs1, Digs2),
  digitsum( D1, D2, C2, D, C, Digs2, Digs).
digitsum( D1, D2, C1, D, C, Digs1, Digs)  :-
  del_var( D1, Digs1, Digs2),        % Select an available digit for D1
  del_var( D2, Digs2, Digs3),        % Select an available digit for D2
  del_var( D, Digs3, Digs),          % Select an available digit for D
  S  is  D1 + D2 + C1,    
  D  is  S mod 10,                   % Reminder
  C  is  S // 10.                    % Integer division
del_var( A, L, L) :-
  nonvar(A), !.                      % A already instantiated
del_var( A, [A|L], L).
del_var( A, [B|L], [B|L1])  :-
  del_var(A, L, L1).
% Some puzzles
puzzle1( [D,O,N,A,L,D],
         [R,O,B,E,R,T] ).
puzzle2( [0,S,E,N,D],
         [M,O,N,E,Y] ).


pl/prolog/pllib/cryptoarithmetic_puzzle_2.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna) Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0