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.


2 responses 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.

  2. geoffrounce 14 September 2017 at 3:47 pm

    Another triangular number puzzle solution:

    % A Solution in MiniZinc
    include "globals.mzn";
    % Grid used
    % A B C   
    % D E F   
    % G H I  
    var 1..9:A;  var 1..9:B;  var 1..9:C;  var 1..9:D;   var 0..9:E;  
    var 0..9:F;  var 1..9:G;  var 0..9:H;  var 0..9:I;    
    %  Triangular numbers across
    var 100..999: ABC = 100*A + 10*B + C;
    var 100..999: DEF = 100*D + 10*E + F;
    var 100..999: GHI = 100*G + 10*H + I;
    set of int: tri3 = { n * (n+1) div 2 | n in 14..44 };
    % Grid numbers across are different 3-digit triangular numbers
    constraint ABC in tri3 /\ DEF in tri3 /\ GHI in tri3
    /\ all_different ( [ ABC, DEF, GHI ] ) /\ increasing ( [ ABC,DEF,GHI ] );
    % The sum of the digits of each of the six numbers must be the same
    constraint all_equal ( [A+B+C, D+E+F, G+H+I, A+D+G, B+E+H, C+F+I] );
    solve satisfy;
    output ["Three triangular numbers are " ++  show(ABC) ++ ", " ++ show (DEF)
     ++ " and " ++ show(GHI) ];
    % Three triangular numbers are 276, 528 and 861
    % ----------
    % Finished in 67msec

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: