Enigmatic Code

Programming Enigma Puzzles

Enigma 1389: Triangular digits

From New Scientist #2549, 29th April 2006

Put a digit in each square of a three-by-three grid so that there are three three-digit numbers across and three three-digit numbers down. The sum of the digits of each of the six numbers must be the same.

The “across” numbers must be three different triangular numbers. Triangular numbers are those that fit the formula n(n+1)/2, like 1, 3, 6, 10, 15. Your numbers must not start with a zero.

List your three triangular numbers in ascending order.



One response to “Enigma 1389: Triangular digits

  1. Jim Randell 29 September 2013 at 8:22 am

    This Python code runs in 35ms.

    from collections import defaultdict
    from itertools import combinations
    from enigma import irange, T, split, printf
    # record 3-digit triangular numbers by digit sum
    # (from trirt(100) to trirt(999))
    ss = defaultdict(list)
    for i in irange(14, 44):
      t = T(i)
      ss[sum(split(t, int))].append(str(t))
    # choose a sum with three or more tris
    for (s, ts) in ss.items():
      if len(ts) < 3: continue
      # choose three of those tris
      for (a, b, c) in combinations(ts, 3):
        # the top one can't contain a zero
        if '0' in a and '0' in b and '0' in c: continue
        # slice the numbers up, and determine their sums
        vs = set(sum(int(x) for x in v) for v in zip(a, b, c))
        # they should all sum to s
        if len(vs) == 1 and s in vs:
          printf("{a} {b} {c} [s={s}]")

    Solution: The three triangular numbers are 276, 528 and 861.

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: