Enigmatic Code

Programming Enigma Puzzles

Puzzle 72: Addition: letters for digits

From New Scientist #1123, 5th October 1978 [link]

In the addition sum below, letters have been substituted for digits. The same latter stands for the same digit whenever it appears and different letters stand for different digits.

Write the sum out with numbers substituted for letters.

[puzzle72]

Advertisements

3 responses to “Puzzle 72: Addition: letters for digits

  1. Jim Randell 31 May 2017 at 11:42 am

    A straightforward alphametic sum. We can feed it to the SubstitutedSum() solver from the enigma.py library.

    This command executes in 170ms.

    % python -m enigma SubstitutedSum "PXHA + PEXA + AHHA + PPFA = FBRP"
    (PXHA + PEXA + AHHA + PPFA = FBRP)
    (2413 + 2043 + 3113 + 2293 = 9862) / A=3 B=8 E=0 F=9 H=1 P=2 R=6 X=4
    

    Solution: The correct sum is: 2413 + 2043 + 3113 + 2293 = 9862.

  2. geoffrounce 31 May 2017 at 1:17 pm
    from itertools import permutations
    
    for q in permutations ('1234567890', 5):
      p, x, h, e, a = q
      if p == '0' or a == '0': continue
      pxha = int(p + x + h + a)
      pexa = int(p + e + x + a)
      ahha = int(a + h + h + a)
      
      qs = set('1234567890'). difference(q)
      for p2 in permutations(qs,3):
          f, b, r = p2
          if f == '0': continue
          ppfa  = int(p + p + f + a)
          fbrp = int( f + b + r + p)
          if pxha + pexa + ahha + ppfa == fbrp:
              print('{} + {} + {} + {} = {}' \
                    .format(pxha, pexa, ahha, ppfa, fbrp))
    
  3. geoffrounce 4 June 2017 at 3:14 pm
    % A Solution in MiniZinc - based on column addition
    include "globals.mzn";
    
    var 1..9:P;  var 0..9:X;  var 0..9:H;  var 1..9:A;  var 0..9:E;
    var 1..9:F;  var 0..9:B;  var 0..9: R; 
    
    var 0..9:carry1;  var 0..9:carry2;  var 0..9:carry3;  
    
    constraint all_different ([P,X,H,A,E,F,B,R]);
    
    % column 1 (from right)
    constraint 4*A mod 10 == P /\ 4*A div 10 = carry1;
    
    % column 2
    constraint (2*H + X + F + carry1) mod 10 == R;
    constraint (2*H + X + F + carry1)  div 10 == carry2;
    
    % column 3
    constraint (X + E + H + P + carry2) mod 10 == B;
    constraint (X + E + H + P + carry2) div 10 == carry3;
    
    % column 4
    constraint 3*P + A + carry3 == F;
    
    solve satisfy;
    
    output [ show(P),show(X),show(H),show(A) ++ " + " ++
             show(P),show(E),show(X),show(A) ++ " + " ++
             show(A),show(H),show(H),show(A) ++ " + " ++
             show(P),show(P),show(F),show(A) ++ " = " ++
             show(F),show(B),show(R),show(P) ];
    
    % 2413 + 2043 + 3113 + 2293 = 9862
    % Finished in 131msec
    

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: