Enigmatic Code

Programming Enigma Puzzles

Enigma 254: Triangular addition

From New Scientist #1401, 15th March 1984 [link]

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

Enigma 254

Write the sum out with numbers substituted for digits.

[enigma254]

Advertisements

4 responses to “Enigma 254: Triangular addition

  1. Jim Randell 29 January 2015 at 8:11 am

    This is exactly the type of puzzle that I wrote the SubstitutedSum() for the enigma.py library. It makes short work of this problem – the following Python code runs in 42ms.

    I’ve recently added the go() method to the SubstitutedSum() and SubstitutedDivision() solvers, so that problems like this can be solved with a single line of code.

    from enigma import SubstitutedSum
    
    SubstitutedSum(['G', 'CG', 'CQG', 'GHCG', 'THXQG'], 'HPBHT').go()
    

    Solution: The numerical sum is: 9 + 89 + 839 + 9689 + 56439 = 67065.

    • geoffrounce 29 January 2015 at 9:46 am
      from itertools import permutations
      
      for p in permutations('9876543210',8):
        c,q,g,h,t,x,p,b = p
        if all(z != '0' for z in(g,c,t,h)):
          G,CG,CQG = int(g),int(c+g), int(c+q+g)
          GHCG,THXQG = int(g+h+c+g), int(t+h+x+q+g)
          HPBHT = int(h+p+b+h+t)
          if G + CG + CQG + GHCG + THXQG == HPBHT:
            print('{}+{}+{}+{}+{}={}'.format(G,CG,CQG,GHCG,THXQG,HPBHT))
      
      • geoffrounce 30 August 2017 at 8:28 am
        % A Solution in MiniZinc
        include "globals.mzn";
         
        var 0..9:G;   var 0..9:C;   var 0..9:Q;   var 0..9:H;   
        var 0..9:T;   var 0..9:X;   var 0..9:P;   var 0..9:B;   
        
        constraint G != 0 /\ C != 0 /\ T != 0 /\ H != 0;
        constraint all_different( [G, C, Q, H, T, X, P, B] );
        
        var 10..99: CG = 10*C + G;
        var 100..999: CQG = 100*C + 10*Q + G;
        var 1000..9999: GHCG = 1000*G + 100*H + 10*C + G;
        var 10000..99999:THXQG = 10000*T + 1000*H + 100*X + 10*Q + G;
        var 10000..99999: HPBHT = 10000*H + 1000*P + 100*B + 10*H + T;
        
        constraint G + CG + CQG + GHCG + THXQG = HPBHT;
        
        solve satisfy;
        
        output ["Sum is " ++ show(G) ++ " + " ++ show(CG) ++ " + " ++ show(CQG) ++ " + "
        ++ show(GHCG) ++ " + " ++ show(THXQG) ++ " = " ++ show(HPBHT) ];
        
        % Sum is 9 + 89 + 839 + 9689 + 56439 = 67065
        % ------------------
        % Finished in 58msec
        
  2. Hugh Casement 30 January 2015 at 11:17 am

    I do find it shows singular lack of imagination to use a random jumble of letters in these substitution puzzles.  I know it makes no difference to either the solution or the method, but wouldn’t it be more interesting with real words?  Not always possible to find valid words for all the numbers, of course, but they could at least try.  For example:

        T
       AT
      ART
     THAT
    SHORT
    HIGHS

    Ars longa, vita brevis!

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: