Enigmatic Code

Programming Enigma Puzzles

Enigma 1174: Small sums

From New Scientist #2330, 16th February 2002 [link]

An example of a letter sum is:

ABC + DCEF = AEFB.

That is to say word + word = word, where each word is a string of letters of any length. An example of an answer for the above sum is:

537 + 4726 = 5263.

We are concerned with letter sums that have exactly one answer. For each such letter sum we produce its A-number by taking its answer and discarding the + and =. For example if the above letter sum had only the answer given above then its A-number would be:

53747265263.

What are the four smallest A-numbers we can get by this process?

[enigma1174]

Advertisements

One response to “Enigma 1174: Small sums

  1. Jim Randell 28 March 2016 at 7:45 am

    This Python program uses the SubstitutedSum() solver from the enigma.py library. It runs in 4.5s.

    from itertools import count, combinations
    from enigma import irange, SubstitutedSum, first, printf
    
    def generate():
      # consider possible A numbers (must be at least 3 digits)
      for A in count(100):
        s = str(A)
    
        # choose locations for + and = to make a + b = c
        for (i, j) in combinations(irange(1, len(s) - 1), 2):
          (a, b, c) = (s[:i], s[i:j], s[j:])
          # ignore cases with leading zeros
          if any(len(x) > 1 and x[0] == '0' for x in (a, b, c)): continue
          # we're only interested in cases where a + b = c
          if int(a) + int(b) != int(c): continue
    
          # look for other solutions
          p = SubstitutedSum([a, b], c)
          try:
            r = first(p.solve(), count=2)
          except AssertionError:
            continue
    
          if len(r) == 1:
            printf("[{A}: {a} + {b} = {c}]")
            yield A
    
    import sys
    N = (4 if len(sys.argv) < 2 else int(sys.argv[1]))
    
    # we want the first N answers
    r = list(first(generate(), N))
    printf("first {N} = {r}")
    

    Solution: The first four A-numbers are 1910, 9110, 89998, 98998.

    The corresponding single solution sums are:

    A + B = AC / 1 + 9 = 10
    A + B = BC / 9 + 1 = 10
    AB + B = BA / 89 + 9 = 98
    A + BA = AB / 9 + 89 = 98

    These are the only A-numbers less than 6 digits.

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: