Enigmatic Code

Programming Enigma Puzzles

Enigma 1135: Perfect harmony

From New Scientist #2291, 19th May 2001 [link]

The Ancient Greeks studied “harmonic triads” of integers, the simplest of which is {2, 3, 6}. The middle number is known as the “harmonic mean” of the other two and is calculated so that its reciprocal is the mean (“average”) of the reciprocals of the other two numbers. George has constructed a “harmonic square” — nine different integers in a 3 × 3 grid such that in each of the three rows, the three columns and the two diagonals, the three numbers form a harmonic triad with the harmonic mean in the middle.

Using his computer, George has discovered that the smallest number which can appear in such a square is 210. You should need only progressive intuition to deduce the other eight numbers which accompany 210 in a harmonic square.

What is the largest of these numbers?



One response to “Enigma 1135: Perfect harmony

  1. Jim Randell 26 December 2016 at 8:39 am

    This Python program stops when it finds a solution. It runs in 53ms.

    # suppose the square is:
    #  A B C
    #  D E F
    #  G H I
    # and the harmonic triads are:
    #  (A B C) (D E F) (G H I) (A D G) (B E H) (C F I) (A E I) (C E G)
    # to ignore symmetrical solutions we will require A to be the smallest
    # valued corner (A < C, G, I) and B < D.
    from itertools import count
    from enigma import irange, chunk, join, first, printf
    # check (x, y, z) is a harmonic triad
    def is_harmonic(x, y, z):
      return 2 * x * z == (x + z) * y
    # determine y from (x, z)
    def harmonic_mean(x, z):
      (y, r) = divmod(2 * x * z, x + z)
      return (y if r == 0 else None)
    # determine z from (x, y)
    def harmonic_other(x, y):
      d = 2 * x - y
      if d < 1: return None
      (z, r) = divmod(x * y, d)
      return (z if r == 0 else None)
    # find harmonic squares with mimimum value m
    def square(m):
      # so A = m
      A = m
      # consider values for D (A < D)
      for D in count(A + 1):
        # A and D define G
        G = harmonic_other(A, D)
        if G is None: continue
        # consider values for B (A < B < D)
        for B in irange(A + 1, D - 1):
          # A and B define C
          C = harmonic_other(A, B)
          if C is None or C in (D, G): continue
          # C and G define E
          E = harmonic_mean(C, G)
          if E is None: continue
          # D and E define F
          F = harmonic_other(D, E)
          if F is None or F == G: continue
          # B and E define H
          H = harmonic_other(B, E)
          if H is None or H == F: continue
          # G and H define I
          I = harmonic_other(G, H)
          if I is None: continue
          # verify the remaining triads
          if not(is_harmonic(C, F, I) and is_harmonic(A, E, I)): continue
          yield (A, B, C, D, E, F, G, H, I)
    # find a harmonic square with a minimum value of 210
    for s in first(square(210)):
      # output the solution
      printf("max = {m} [{s}]", m=max(s), s=join(chunk(s, 3), sep=' / '))

    Solution: The largest number in the harmonic square is 1260.

    The magic square is:

    A little bit of analysis allows us to put an upper bound on D of (2×A − 1) = 419, so we can modify line 39 to have an explicit range. This lets us exhaustively search the problem space in 68ms. There are no further solutions.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: