Enigmatic Code

Programming Enigma Puzzles

Enigma 1647: Fifth-division clincher

From New Scientist #2813, 21st May 2011 [link]

ENIGMA is a six-digit number in which all the digits are different and non-zero. When ENIGMA is divided by 19, or 17, or 13, or 11, or G, the remainders are, respectively, 17, 13, 11, 7 and I (I as in India).

Find ENIGMA.

[enigma1647]

Advertisements

One response to “Enigma 1647: Fifth-division clincher

  1. Jim Randell 7 December 2011 at 9:48 am

    A little bit of analysis yields this program, which runs in 30ms:

    # consider ENIGMA + 2, it must be a factor of 19 and 13 (247)
    # and ENIGMA + 4 must be a factor of 17 and 11 (187)
    
    for ENIGMA2 in range(100035, 1000103, 247):
      ENIGMA4 = ENIGMA2 + 2
      if ENIGMA4 % 187 > 0: continue
      ENIGMA = ENIGMA2 - 2
      # split it into digits
      s = str(ENIGMA)
      d = set(s)
      if len(d) != 6: continue
      if '0' in d: continue
      if ENIGMA % int(s[3]) != int(s[2]): continue
      print("ENIGMA =", ENIGMA)
    

    and it produces the same result as this more straightforward approach (which has a runtime of 200ms):

    from itertools import permutations
    
    for x in permutations(range(1, 10), 6):
      ENIGMA = int(''.join(map(str, x)))
      if ENIGMA % 19 != 17: continue
      if ENIGMA % 17 != 13: continue
      if ENIGMA % 13 != 11: continue
      if ENIGMA % 11 !=  7: continue
      if ENIGMA % x[3] != x[2]: continue
      print("ENIGMA =", ENIGMA)
    

    Solution: ENIGMA = 943785.

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: