Enigmatic Code

Programming Enigma Puzzles

Puzzle 80: Addition: letters for digits

From New Scientist #1131, 30th November 1978 [link]

Below is an addition sum with letters substituted for digits. The same letter stands for the same digit wherever it appears, and different letters stand for different digits:

puzzle-80

Write the sum out with numbers substituted for letters.

[puzzle80]

Advertisements

2 responses to “Puzzle 80: Addition: letters for digits

  1. Jim Randell 22 February 2017 at 10:03 am

    We can solve this puzzle directly from the command line using the SubstitutedSum() solver in the enigma.py library:

    % python -m enigma SubstitutedSum "HCXDD + GCGDD + ECXDD + CXDDD = DDKHDK" 
    (HCXDD + GCGDD + ECXDD + CXDDD = DDKHDK)
    (89433 + 69633 + 79433 + 94333 = 332832) / C=9 D=3 E=7 G=6 H=8 K=2 X=4
    

    The command executes in 85ms.

    Solution: The correct sum is: 89433 + 69633 + 79433 + 94333 = 332832.

  2. geoffrounce 3 June 2017 at 7:38 pm

    I tried a column addition approach in the programme below:

    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:H;  var 1..9:C;  var 0..9:X;  var 1..9:D;  var 1..9:G;
    var 1..9:E;  var 1..9:K;  var 1..9: carry1;  var 1..9: carry2; 
    var 1..9: carry3;  var 1..9: carry4;  var 1..9: carry5; 
    
    constraint all_different ([H,C,X,D,G,E,K]);
    
    % 1st and 2nd columns (from right)
    constraint 4*D mod 10 == K /\ 4*D div 10 == carry1;
    constraint (4*D + carry1) mod 10 == D /\ (4*D + carry1) div 10 == carry2;
    
    % 3rd column
    constraint (2*X + G + D + carry2) mod 10 == H;
    constraint (2*X + G + D + carry2) div 10 == carry3;
    
    % 4th column
    constraint (3*C + X + carry3) mod 10 == K;
    constraint (3*C + X + carry3) div 10 == carry4;
    
    % 5th and 6th columns
    constraint (H + G + E + C + carry4) mod 10 == D;
    constraint (H + G + E + C + carry4) div 10 == D;
    
    solve satisfy;
    
    output [ "HCXDD = " ++ show(H),show(C),show(X),show(D),show(D) ++ "\n" ++
             "GCGDD = " ++ show(G),show(C),show(G),show(D),show(D) ++ "\n" ++
             "ECXDD = " ++ show(E),show(C),show(X),show(D),show(D) ++ "\n" ++
             "CXDDD = " ++ show(C),show(X),show(D),show(D),show(D) ++ "\n" ++ 
             "DDKHDK = " ++ show(D),show(D),show(K),show(H),show(D),show(K) ];
             
    % HCXDD = 89433
    % GCGDD = 69633
    % ECXDD = 79433
    % CXDDD = 94333
    % DDKHDK = 332832
    % ----------
    % Finished in 132msec
    

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: