Enigmatic Code

Programming Enigma Puzzles

Enigma 1503: Division decision

From New Scientist #2665, 19th July 2008

In the long-division sum shown, all odd digits have been replaced by dashes and all even digits by asterisks. There is no remainder.

Enigma 1503

What is the 5-digit dividend?

[enigma1503]

Advertisements

2 responses to “Enigma 1503: Division decision

  1. Jim Randell 21 October 2012 at 8:40 pm

    The following Python program runs in 36ms.

    from enigma import irange, nconcat, printf
    
    # check the number <n> has digits corresponding to the sets <ds>
    def check(n, *ds):
      s = str(n)
      if len(s) != len(ds): return False
      return all(int(a) in b for a, b in zip(s, ds))
    
    odd = set((1, 3, 5, 7, 9))
    evenp = set((2, 4, 6, 8))
    even = evenp.union([0])
    
    # possible values for the divisor
    for a1 in odd:
      for a2 in odd:
        aa = nconcat(a1, a2)
      
        # work through each digit in the dividend
        for c1 in odd:
          for c2 in odd:
            (b1, r1) = divmod(c1 * 10 + c2, aa)
            if not check(b1, evenp): continue
            if not check(r1, odd, odd): continue
            if not check(b1 * aa, odd, even): continue
    
            for c3 in even:
              (b2, r2) = divmod(r1 * 10 + c3, aa)
              if not check(b2, odd): continue
              if not check(r2, odd): continue
              if not check(b2 * aa, odd, even, odd): continue
    
              for c4 in odd:
                (b3, r3) = divmod(r2 * 10 + c4, aa)
                if not check(b3, odd): continue
                if not check(r3, odd, even): continue
                if not check(b3 * aa, odd, odd): continue
    
                for c5 in even:
                  (b4, r4) = divmod(r3 * 10 + c5, aa)
                  if not check(b4, even): continue
                  if r4 != 0: continue
                  if not check(b4 * aa, odd, even, even): continue
                  
                  printf("{c1}{c2}{c3}{c4}{c5} / {aa} = {b1}{b2}{b3}{b4}")
    

    Solution: The five-figure dividend is 93018.

  2. geoffrounce 14 December 2017 at 11:20 am
    % A Solution in MiniZinc
    include "globals.mzn";
    
    %      MiniZinc Model         Solution
    %      --------------         --------
    %
    %          H I J K              2 5 1 4   
    %        ---------           ---------
    %   F G )A B C D E        3 7)9 3 0 1 8
    %        L M                  7 4
    %        ---                  -----
    %        N P C                1 9 0
    %        R S T                1 8 5
    %        -----                -----
    %            U D                  5 1
    %            W X                  3 7
    %            -----                -----
    %            Y Z E                1 4 8
    %            a b c                1 4 8
    %            =====                =====
    
    set of int: odds =  {1,3,5,7,9};
    set of int: evens = {0,2,4,6,8};
    
    % All odd digits: I,J,F,G,A,B,D,L,N,P,R,T,U,W,X,Y,a
    var odds: I;  var odds: J;  var odds: F;  var odds: G;  var odds:A;
    var odds: B;  var odds: D;  var odds: L;  var odds: N;  var odds:P;
    var odds: R;  var odds: T;  var odds: U;  var odds: W;  var odds:X;
    var odds: Y;  var odds:a;
    
    % All even digits: H,K,C,E,M,S,Z,b,c 
    var evens: H;  var evens:  K;  var evens: C;  var evens: E;  var evens:M;
    var evens: S;  var evens:  Z;  var evens: b;  var evens: c;
    
    constraint H != 0 /\ F != 0 /\ L != 0 /\ N != 0
    /\ R != 0 /\ U != 0 /\ U != 0 /\ W != 0 /\ Y != 0 /\ a != 0;
    
    var 10..99: FG = 10*F + G;   
    var 1000..9999: HIJK = 1000*H + 100*I + 10*J + K;   
    var 10000..99999: ABCDE = 10000*A + 1000*B + 100*C + 10*D + E;   
    var 10..99: AB = 10*A + B;
    var 10..99: LM = 10*L + M;
    var 10..99: NP = 10*N + P;
    var 100..999: NPC = 100*N + 10*P + C;
    var 100..999: RST = 100*R + 10*S + T;
    var 10..99: UD = 10*U + D;
    var 10..99: WX = 10*W + X;
    var 10..99: YZ = 10*Y + Z;
    var 100..999: YZE = 100*Y + 10*Z + E ;
    var 100..999: abc = 100*a + 10*b + c;
    
    % multiplication sums
    constraint FG * HIJK = ABCDE;
    constraint H * FG = LM;
    constraint I * FG = RST;
    constraint J * FG = WX;
    constraint K * FG = abc;
    
    % subtractions sums
    constraint AB - LM = NP;
    constraint NPC - RST = U;
    constraint UD - WX = YZ;
    constraint YZE - abc = 0;
    
    solve satisfy;
    
    % Output
    % I = 5;  J = 1;  F = 3;  G = 7;  A = 9; B = 3;  D = 1;  L = 7;  N = 1;  P = 9;
    % R = 1;  T = 5;  U = 5;  W = 3;  X = 7; Y = 1;  a = 1;  H = 2;  K = 4;  C = 0;
    % E = 8;  M = 4;  S = 8;  Z = 4;  b = 4;  c = 8;
    %
    % Finished in 70msec
    
    

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

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

%d bloggers like this: