Enigmatic Code

Programming Enigma Puzzles

Enigma 1451: Odds and evens

From New Scientist #2612, 14th July 2007

I have created a sum by adding a three-digit number consisting solely of odd digits to a three-digit number consisting solely of even digits to make a three-digit number in which odd and even digits alternate. The nine digits used are all different. If I tell you any of my three numbers you will be able to deduce with certainty what the other two numbers are.

What are the three numbers of my sum?

Enigma 1721 is also called “Odds and evens”.

[enigma1451]

Advertisements

One response to “Enigma 1451: Odds and evens

  1. Jim Randell 15 March 2013 at 9:03 am

    This Python program runs in 63ms.

    from itertools import product, permutations
    from collections import Counter
    from enigma import nconcat, split, printf
    
    evens = (0, 2, 4, 6, 8)
    odds  = (1, 3, 5, 7, 9)
    
    # record the sums, and count uses of each number
    sums = set()
    used = Counter()
    # t1 is 3 different odd digits
    for s1 in permutations(odds, 3):
      t1 = nconcat(s1)
      # t2 is 3 different even digits
      for s2 in permutations(evens, 3):
        if s2[0] == 0: continue
        t2 = nconcat(s2)
        # their sum is a 3 digit number
        r = t1 + t2
        rs = list(split(r, int))
        if len(rs) != 3: continue
        # the digits used in numbers are all different
        if len(set(rs).union(s1, s2)) != 9: continue
        # and the sum alternates odd and even digits
        if not((rs[0] in evens and rs[1] in odds and rs[2] in evens) or
               (rs[0] in odds and rs[1] in evens and rs[2] in odds)): continue
        printf("[{t1} + {t2} = {r}]")
        sums.add((t1, t2, r))
        used.update((t1, t2, r))
    
    # find a sum where each number is unique to that sum
    for a, b, c in sums:
      if used[a] == used[b] == used[c] == 1:
        printf("{a} + {b} = {c}")
    

    Solution: The three numbers in your sum are 359, 402 and 761.

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: