Enigmatic Code

Programming Enigma Puzzles

Enigma 1420: a² + b² + c²

From New Scientist #2580, 2nd December 2006

This puzzle is concerned with the equation a² + b² + c² = d² + e² + f², where a, b, c, d, e, and f are different integers between 1 and 9.

I found a solution to that equation. I then altered just one of the integers on each side of that solution to create a second solution. I then altered just one of the integers on each side of the second solution to create a third solution. I then altered just one of the numbers on each side of the third solution to create a fourth solution. All four solutions were different.

If the fourth solution produced a larger sum than the first solution, what was the value of (a² + b² + c²) in each of the four solutions in order?

[enigma1420]

Advertisements

One response to “Enigma 1420: a² + b² + c²

  1. Jim Randell 17 June 2013 at 9:33 am

    This Python program runs in 40ms.

    from collections import defaultdict
    from itertools import combinations, permutations
    from enigma import irange, printf
    
    # record triples by the sum of their squares
    t = defaultdict(list)
    ds = set(irange(1, 9))
    for (a, b, c) in combinations(ds, 3):
      s = a * a + b * b + c * c
      t[s].append((a, b, c))
    
    # we need sums with multiple triples
    t = dict((k, v) for (k, v) in t.items() if len(v) > 1)
    
    # check the pair of triples e can be added to the chain of equations eqs
    def check(eqs, e):
      (e0, e1) = e
      # numbers must be distinct amongst the triples
      if set(e0).intersection(e1): return False
      # check the triples differ from the previous ones by a single value
      n = len(eqs)
      if n == 0: return (e0 < e1) # for uniqueness
      if any(len(set(e[i]).intersection(eqs[-1][i])) != 2 for i in (0, 1)): return False
      # and check this equation is different from all previous equations
      if n == 1: return True
      if sorted(e) in (sorted(eq[0:2]) for eq in eqs[:n - 1]): return False
      return True
    
    # eqs = list of (t1, t2, s), where s is the sum of triples t1, t2
    def solve(eqs, t):
      # are we done?
      if len(eqs) == 4:
        if eqs[0][2] < eqs[-1][2]:
          printf("{s} {eqs}", s=tuple(e[2] for e in eqs))
        return
      # chose a pair of triples for the next equation
      for (k, v) in t.items():
        for e in permutations(v, 2):
          if check(eqs, e):
            solve(eqs + [e + (k,)], t)
    
    solve([], t)
    

    Solution: The values of a² + b² + c² are 62, 77, 122, 98.

    The equations are:

    (1) 1² + 5² + 6² = 2² + 3² + 7² = 62
    (2) 4² + 5² + 6² = 2² + 3² + 8² = 77
    (3) 4² + 5² + 9² = 7² + 3² + 8² = 122
    (4) 4² + 1² + 9² = 5² + 3² + 8² = 98

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: