Enigmatic Code

Programming Enigma Puzzles

Enigma 1603: A double sum

From New Scientist #2768, 10th July 2010 [link]

In the addition sums above, different letters stand for different digits. The same letter consistently stands for the same digit, each emoticon can be any digit, and leading digits cannot be zero.

What number is FOURTEEN?



One response to “Enigma 1603: A double sum

  1. jimrandell 8 January 2012 at 2:43 pm

    This Python program runs in 54ms. It uses the second sum to reduce the number of possibilities for the assignment of the letters, and then looks for example solutions for both equations to come up with the solution.

    # A) ??????
    #    ??????
    #    FOUR??
    #  + SEVEN?
    #    ------
    #  = ELEVEN
    # B) ?SEVEN??
    #  + ?SEVEN??
    #    --------
    #  = FOURTEEN
    from itertools import permutations
    from enigma import concat, split, printf
    # check to see if sum A can be made
    def checkA(FOUR, SEVEN, ELEVEN):
      # the missing digit in SEVEN?
      for x in range(10):
        SEVENx = 10*SEVEN + x
        # the missing digits in FOUR??
        for xx in range(100):
          FOURxx = 100*FOUR + xx
          # which means the first two lines must be
          n = ELEVEN - FOURxx - SEVENx
          # n must be 5 digits, and greater than 100000 + 100000
          if 199999 < n < 1000000:
            printf("[A] {n} + {FOURxx} + {SEVENx} = {ELEVEN}")
            return True
      return False
    # check to see if sum B can be made
    def checkB(SEVEN, FOURTEEN):
      # the leading digit of the first line
      for x in range(1, 10):
        # the last two digits
        for xx in range(0, 100):
          a = int(concat(x, SEVEN, xx))
          b = FOURTEEN - a
          # check b is 8 digits and the middle of b is SEVEN
          if 9999999 < b < 100000000 and (b // 100) % 100000 == SEVEN:
            printf("[B] {a} + {b} = {FOURTEEN}")
            return True
      return False
    def generate():
      # generate the letters (in alphabetical order)
      # E, F, S must be non zero
      d0 = set(range(10))
      for (E, N, S, V) in permutations(d0, 4):
        if 0 in (E, S): continue
        # work out the carry to the 100's column of B
        if (N+N) % 10 == E:
          c = 0
        elif (N+N+1) % 10 == E:
          c = 1
        # check the middle section of B
        SEVEN = int(concat(S, E, V, E, N))
        OURTE = (SEVEN + SEVEN + c) % 100000
        (O, U, R, T, e) = split("{:05d}".format(OURTE), int)
        d1 = d0.difference((E, N, O, R, S, T, U, V))
        if len(d1) != 2: continue
        # F, L are what is left
        for (F, L) in permutations(d1, 2):
          if F < 2: continue # from LH column of B
          yield (E, F, L, N, O, R, S, T, U, V)
    for (E, F, L, N, O, R, S, T, U, V) in generate():
      # check the sums are possible
      SEVEN = int(concat(S, E, V, E, N))
      ELEVEN = int(concat(E, L, E, V, E, N))
      FOUR = int(concat(F, O, U, R))
      if not checkA(FOUR, SEVEN, ELEVEN): continue
      FOURTEEN = int(concat(FOUR, T, E, E, N))
      if not checkB(SEVEN, FOURTEEN): continue
      printf("FOURTEEN ={FOURTEEN}")

    Solution: FOURTEEN = 35617889.

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: