Enigmatic Code

Programming Enigma Puzzles

Enigma 1405: Digitally divided

From New Scientist #2565, 19th August 2006

Find a 2-digit integer, a 3-digit integer and a 4-digit integer that between them use all the digits 1-9 in such a way that each integer is divisible by each of its digits; in addition the sum of the three integers must be a 4-digit number that consists of four different digits and is itself divisible by each of its four digits.

What are the three integers?

[enigma1405]

4 responses to “Enigma 1405: Digitally divided

  1. Jim Randell 6 August 2013 at 8:49 am

    This Python program runs in 55ms.

    from itertools import permutations
    from enigma import irange, nconcat, printf
    
    
    # n-digit numbers that divide their digits
    def numbers(n):
      for ds in permutations(irange(1, 9), n):
        if ds[0] == 0: continue
        i = nconcat(ds)
        if all(i % d == 0 for d in ds):
          yield i
    
    n2s = set(numbers(2))
    n3s = set(numbers(3))
    n4s = set(numbers(4))
    
    for n2 in n2s:
      for n3 in n3s:
        if len(set(str(n2) + str(n3))) != 5: continue
        for n4 in n4s:
          if len(set(str(n2) + str(n3) + str(n4))) != 9: continue
          s = n2 + n3 + n4
          ss = str(s)
          if len(ss) != 4 or len(set(ss)) != 4 or '0' in ss: continue
          if any(s % int(d) > 0 for d in ss): continue
    
          printf("n2={n2} n3={n3} n4={n4} [s={s}]")
    

    Solution: The three integers are 48, 672 and 3195.

  2. geoffrounce 6 August 2013 at 8:01 pm

    Here is my solution:

    from itertools import permutations
    for p in permutations(range(1,10)):
        a,b,c,d,e,f,g,h,i = p
        # two digit number
        n2 = a + b*10
        if n2 % a == 0 and n2 % b == 0:
            # three digit number
            n3 = c + d*10 + e*100
            if all(n3 % x == 0 for x in (c,d,e)):
                # first four digit number
                n4 = f + g*10 + h*100 + i*1000
                if all(n4 % x == 0 for x in (f,g,h,i)):
                    # second four digit number
                    n5 = n2 + n3 + n4
                    if n5 > 1000 and n5 < 10000:
                        str_n5 = str(n5)
                        set_n5 = set(str_n5)
                        if len(set_n5) == 4:
                            j,k = int(str_n5[0]),int(str_n5[1])
                            l,m = int(str_n5[2]),int(str_n5[3])
                            if all(n5 % x == 0 for x in (j,k,l,m)):
                                print('n2, n3, n4, sum = ',n2,n3,n4,n5)
    
    
  3. Jim Randell 31 March 2017 at 8:15 am

    We can solve this puzzle using the [[ SubstitutedExpression() ]] solver from the enigma.py library. (You will need version 2017-03-24 or later to use the extended [[ distinct ]] parameter – see my comment on Enigma 389).

    The following run file executes in 83ms.

    #!/usr/bin/env python -m enigma -r
    
    # let:
    # the 2-digit number = AB
    # the 3-digit number = CDE
    # the 4-digit number = FGHI
    
    SubstitutedExpression
    
    --symbols="ABCDEFGHIPQRS"
    --distinct="ABCDEFGHI,PQRS"
    --digits="1-9"
    --answer="(AB, CDE, FGHI)"
    
    "AB % A = 0"
    "AB % B = 0"
    
    "CDE % C = 0"
    "CDE % D = 0"
    "CDE % E = 0"
    
    "FGHI % F = 0"
    "FGHI % G = 0"
    "FGHI % H = 0"
    "FGHI % I = 0"
    
    "AB + CDE + FGHI = PQRS"
    
    "PQRS % P = 0"
    "PQRS % Q = 0"
    "PQRS % R = 0"
    "PQRS % S = 0"
    
  4. geoffrounce 31 March 2017 at 9:51 am

    The two four digit numbers contain the same digits, although this is not a stated constraint

    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:A;  var 1..9:B;  var 1..9:C;  var 1..9:D;  var 1..9:E; 
    var 1..9:F;  var 1..9:G;  var 1..9:H;  var 1..9:I;  
    var 1..9:J;  var 1..9:K;  var 1..9:L;  var 1..9:M;
    
    constraint alldifferent([A,B,C,D,E,F,G,H,I]);
    
    % Form a 2-digit integer, a 3-digit integer and two 4-digit integers
    var 10..98: AB = 10*A + B;
    var 100..999: CDE = 100*C + 10*D + E;
    var 1000..9999: FGHI = 1000*F + 100*G + 10*H + I;
    var 1000..9999: JKLM = 1000*J + 100*K + 10*L + M;
    
    constraint sum ([AB mod A == 0, AB mod B == 0]) == 2;
    constraint sum ([CDE mod C == 0, CDE mod D == 0, CDE mod E == 0]) == 3;
    constraint sum ([FGHI mod F == 0, FGHI mod G == 0, FGHI mod H == 0, FGHI mod I == 0]) == 4;
    
    constraint AB + CDE + FGHI == JKLM /\ alldifferent([J,K,L,M]);
    constraint sum ([JKLM mod J == 0, JKLM mod K == 0, JKLM mod L == 0, JKLM mod M == 0]) == 4;
    
    solve satisfy;
    
    output ["The three integers are " ++ show([AB,CDE,FGHI]) 
            ++ " and the fourth number is " ++ show(JKLM) ];
            
    % The three integers are [48, 672, 3195] and the fourth number is 3915
    % Finished in 115msec
    

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 )

Connecting to %s

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

%d bloggers like this: