Enigmatic Code

Programming Enigma Puzzles

Enigma 1621: Times square

From New Scientist #2786, 13th November 2010 [link]

Penny thinks she knows everything about “Magic” squares, so Joe suggested she might design a three-by-three “Times” square, where the products of the three numbers in each row, column and main diagonal are all the same. The only other requirement was that all the numbers had to be different integers. Penny found there are many such squares with different products.

What is the smallest product for a three-by-three “Times” square?

[enigma1621]

Advertisements

One response to “Enigma 1621: Times square

  1. jimrandell 21 December 2011 at 8:38 am

    The following Python code runs in 65ms.

    # a b c
    # d e f
    # g h i
    
    from itertools import count
    from enigma import is_distinct, divisors
    
    def solve():
      # let p = a * b * c
      for p in count(6):
        ds = divisors(p)
        for a in ds:
          for c in ds:
            if not(a < c): continue
            (b, r) = divmod(p, a * c)
            if r > 0: continue
            if not is_distinct(b, a, c): continue
            # so e has to be a divisor of p
            for e in ds:
              if not is_distinct(e, a, b, c): continue
              # h = p/be
              (h, r) = divmod(p, b * e)
              if r > 0: continue
              if not is_distinct(h, a, b, c, e): continue
              # i = p/ae
              (i, r) = divmod(p, a * e)
              if r > 0: continue
              if not is_distinct(i, a, b, c, e, h): continue
              # g = p/ce
              (g, r) = divmod(p, c * e)
              if r > 0: continue
              if not is_distinct(g, a, b, c, e, h, i): continue
              if g * h * i != p: continue
              # d = p/ag
              (d, r) = divmod(p, a * g)
              if r > 0: continue
              if not is_distinct(d, a, b, c, e, g, h, i): continue
              # f = p/de
              (f, r) = divmod(p, d * e)
              if r > 0: continue
              if not is_distinct(f, a, b, c, d, e, g, h, i): continue
              if not(c * f * i == p): continue
    
              print(p, (a, b, c, d, e, f, g, h, i))
              return
    
    solve()
    

    Solution: The smallest product is 216.

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: