Enigmatic Code

Programming Enigma Puzzles

Enigma 16: Four, five, six, seven

From New Scientist #1158, 7th June 1979 [link] [link]

“A pattern, that’s what I like”, said Uncle Bungle. “I can’t stand a puzzle that is all over the place. But in this long division sum the four figures that are given have the great merit of being consecutive”. I could see my uncle’s point all right, but unfortunately (and this will not I think surprise my readers) his achievements did not come up to his hopes and expectations. I can hardly bear to tell my readers this, but in fact one of those figures was wrong.

The puzzle, as Uncle Bungle gave it to me, looked like this:

Enigma 16

Which figure was wrong? Find the correct division sum.

[enigma16]

5 responses to “Enigma 16: Four, five, six, seven

  1. Jim Randell 12 December 2012 at 12:05 am

    The following Python program runs in 45ms.

    from enigma import irange, printf
    
    # how many of the items in the iterable are False
    def check(*i):
      return sum(1 for x in i if not(x))
    
    # we need a * b = c where a is a 2-digit number, b is a 3-digit number
    # (with a 0 in the middle), and c is a 4-digit number
    for a in irange(10, 99):
      for b3 in irange(0, 9):
        for b1 in irange(1, 9):
          b = b1 * 100 + b3
          c = a * b
          if not(c < 10000): break
          # check the digits filled out
          a1 = a // 10
          c1 = c // 1000
          # at most one of these can be false
          (s1, s2) = ((a1 == 4), (c1 == 6))
          if check(s1, s2) > 1: continue
          # and check the intermediate values
          d = a * b1
          if not(9 < d < 100): continue
          d2 = d % 10
          # at most one of these can be false
          s3 = (d2 == 7)
          if check(s1, s2, s3) > 1: continue
          e1 = (c // 100) - d
          if not(0 < e1 < 10): continue
          # exactly one of the these has to be false
          s4 = (e1 == 5)
          if check(s1, s2, s3, s4) != 1: continue
    
          printf("{a} x {b} = {c} [4:{s1}, 6:{s2}, 7:{s3}, 5:{s4}]")
    

    Solution: The figure 4 is wrong, it should be a 5.

    The actual division sum is:

    Enigma 16 - Solution

  2. Jim Randell 13 July 2014 at 10:34 am

    Here’s a solution using the [[ SubstitutedDivision() ]] solver from the enigma.py library. It runs in 49ms.

    from enigma import SubstitutedDivision, printf
    
    # the digits in the sum
    digits = list("4567")
    
    # replacement digits
    replacements = list("4567X")
    
    # make a new array of digits with one of them replaced
    for (i, d) in enumerate(digits):
      for x in replacements:
        if x == d: continue
        ds = list(digits)
        ds[i] = x
    
        # create the sum
        (d4, d5, d6, d7) = ds    
        p = SubstitutedDivision(
          d6 + '???', d4 + '?', '???',
          [('?' + d7, '?'), None, (d5 + '??', '')],
          dict((x, int(x)) for x in set(ds) if x != 'X')
        )
    
        # solve it
        for s in p.solve():
          printf("{d} -> {x}")
          p.solution(s)
    
  3. Jim Randell 20 April 2017 at 4:25 pm

    Here’s a solution using the [[ SubstitutedExpression() ]] general alphametic solver from the enigma.py library.

    We don’t need to write a program, just provide the expressions and options as arguments, here is the relevant run file:

    #!/usr/bin/env python -m enigma -r
    
    # writing the sum as:
    #
    #          G H I
    #      ---------
    #  E F ) A B C D
    #        K L
    #        ---
    #          M C D
    #          M C D
    #          =====
    #
    # we see that H = 0, and exactly one of (E != 4, A != 6, L != 7, M != 5)
    
    # use the alphametic solver
    SubstitutedExpression
    
    # letters do not necessarily stand for distinct digits
    --distinct=""
    # assign H=0
    --assign="H,0"
    
    # the main division sum
    "GHI * EF = ABCD"
    
    # the multiples
    "EF * G = KL"
    "EF * I = MCD"
    
    # the intermediate subtractions
    "AB - KL = M"
    
    # exactly one of these inequalities holds
    "sum([E != 4, A != 6, L != 7, M != 5]) = 1"
    

    It executes in 96ms:

    % python -m enigma -r enigma16.run
    (GHI * EF = ABCD) (EF * G = KL) (EF * I = MCD) (AB - KL = M) (sum([E != 4, A != 6, L != 7, M != 5]) = 1)
    (109 * 57 = 6213) (57 * 1 = 57) (57 * 9 = 513) (62 - 57 = 5) (sum([5 != 4, 6 != 6, 7 != 7, 5 != 5]) = 1) / A=6 B=2 C=1 D=3 E=5 F=7 G=1 H=0 I=9 K=5 L=7 M=5
    [1 solution]
    
  4. Jim Randell 19 July 2017 at 11:28 am

    The new [[ SubstitutedDivision() ]] solver in the enigma.py library is based on the [[ SubstitutedExpression() ]] solver, so we can have an even simpler run file.

    This run file executes in 108ms.

    #!/usr/bin/env python -m enigma -r
    
    # writing the sum as:
    #
    #          ? ? ?
    #      ---------
    #  A ? ) C ? ? ?
    #        ? D
    #        ---
    #          ? ? ?
    #          B ? ?
    #          =====
    #
    # one of the following is wrong:
    #
    #  A = 4, B = 5, C = 6, D = 7
    
    SubstitutedDivision
    
    # substituted digits are not necessarily distinct
    --distinct=""
    
    # the main division sum
    "C??? / A? = ???"
    
    # the intermediate subtraction sums (the second is empty)
    "C? - ?D = ?"
    ""
    "??? - B?? = 0"
    
    # exactly one the following inequalities holds
    --extra="sum([A != 4, B != 5, C != 6, D != 7]) = 1"
    
  5. geoffrounce 23 November 2017 at 9:34 pm
    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:A;   var 0..9:B;  var 0..9:C;   % same letters as given solution
    var 0..9:D;   var 1..9:E;  var 0..9:F;
    var 1..9:G;   var 0..9:H;  var 0..9:I;
    var 1..9:K;   var 0..9:L;  var 1..9:M;
    
    var 1000..9999: ABCD = 1000*A +100*B +10*C + D;
    var 100..999 : MCD = 100*M + 10*C + D;
    var 100..999 : GHI = 100*G + 10*H + I;
    
    var 10..99 : EF = 10 * E + F;
    var 10..99 : KL = 10 * K + L;
    var 10..99 : AB = 10 * A + B;
    
    constraint GHI * EF = ABCD /\ EF * G = KL /\ EF * I = MCD
    /\ AB - KL = M ;
    
    % One digit of 4, 5, 6, or 7 is incorrect
    constraint sum([E == 4, A == 6, L == 7, M == 5]) = 3;
    
    solve satisfy;
    
    output [ show(GHI) ++ " x " ++ show(EF) ++ " = " ++ show(ABCD) ] ++ 
     ["\n" ++ show("A, B, C, D, E, F, G, H, I, K, L, M") ++ " = " 
     ++ show([A, B, C, D, E, F, G, H, I, K, L, M]) ];
    
    % 109 x 57 = 6213
    %   [ A, B, C, D, E, F, G, H, I, K, L, M ] 
    % =  [6, 2, 1, 3, 5, 7, 1, 0, 9, 5, 7, 5]
    % ----------
    % Finished in 58msec
    %
    % Reconstructed division sum from above arrays:
    %           G H I           1 0 9
    %       ---------      ----------
    %   E F ) A B C D      57)6 2 1 3
    %         K L             5 7
    %         ---             ---
    %           M C D           5 1 3
    %           M C D           5 1 3
    %           -----           -----
    % Answer : The digit '4' in the divisor is incorrect and should be 5 
    

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.