Enigmatic Code

Programming Enigma Puzzles

Enigma 1353: Three cubed

From New Scientist #2512, 13th August 2005

Enigma 1353

Harry, Tom and I each looked to replace each asterisk with a digit in such a way that the two 4-digit numbers and one 3-digit number that could be read across and the 5-digit number that could be read down were all perfect cubes (with no leading zeros).

We each found a different solution that used nine of the ten digits 0-9. Harry’s unused digit was the same as Tom’s.

What was my solution?

[enigma1353]

Advertisements

2 responses to “Enigma 1353: Three cubed

  1. Jim Randell 24 January 2014 at 8:38 am

    This Python program runs in 35ms.

    from collections import defaultdict
    from itertools import permutations
    from enigma import irange, printf
    
    # 3, 4, 5 digit cubes
    cubes = defaultdict(list)
    for i in irange(5, 46):
      n = str(i ** 3)
      cubes[len(n)].append(n)
    
    # digits
    ds = set('0123456789')
    
    # find possible arrangements: record (digit, cubes)
    r = list()
    
    # choose a 5 digit cube
    for c5 in cubes[5]:
      # and a 3 digit cube
      for c3 in cubes[3]:
        if c3[-1] != c5[2]: continue
        # and the top 4 digit cube
        for c4t in cubes[4]:
          if c4t[-1] != c5[0]: continue
          # and the bottom 4 digit cube
          for c4b in cubes[4]:
            if c4b[-1] != c5[-1]: continue
            # we're interested in arrangements that use 9 digits
            d = ds.difference(c5 + c3 + c4t + c4b)
            if len(d) != 1: continue
            r.append((d.pop(), (c5, c3, c4t, c4b)))
    
    # find solutions
    for (T, D, H) in permutations(r, 3):
      # H and T's unused digits are the same
      if H[0] == T[0]:
        printf("D={D} [T={T} / H={H}]", D=' '.join(D[1]), T=' '.join(T[1]), H=' '.join(H[1]))
    

    Solution: Dick’s solution is:

    4096
       8
     729
       2
    1331

    There are only three solutions that use exactly 9 digits. The one given above doesn’t use the digit 5, the remaining two solutions (Harry’s and Tom’s) don’t use the digit 8:

    1331    3375
       7       0
     125     216
       7       5
    4096    4913
  2. Naim Uygun 24 January 2014 at 10:43 am
    """
    1331 7 125 7 4096 17576
    The unused digit: {'8'}
    
     3375 0 216 5 4913 50653
    The unused digit: {'8'}
    
    Answer: 4096 8 729 2 1331 68921
    The unused digit: {'5'}
    """
    d=set(str(i) for i in range(0,10))
    cube3=[str(x**3) for x in range(5,10)]
    cube4=[str(x**3) for x in range(10,22)]
    cube5=[str(x**3) for x in range(22,47)]
    for one in cube4:
        for two in cube4:
            for three in cube3 :
                for five in cube5:
                  if one[3] != five[0]: continue
                  if three[2] != five[2]: continue
                  if two[3] != five[4]: continue
                  if '0'==five[0] :continue
                  s=set(list(one+two+three+five))
                  if len(s)!= 9: continue
                  print("\n",one,five[1],three,five[3],two,five)
                  print("The unused digit:",d-s)
    

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: