Enigmatic Code

Programming Enigma Puzzles

Enigma 1136: Triangular numbers

From New Scientist #2292, 26th May 2001 [link]

Triangular numbers are those that fit the formula ½n(n+1), like 1, 3, 6 and 10.

In the following statement digits have been consistently replaced by capital letters, different letters being used for different digits:

“ONE, THREE, SIX and TEN are all triangular numbers, none of which starts with a zero”.

Which numbers are represented (in this order) by ONE, THREE, SIX and TEN?

[enigma1136]

3 responses to “Enigma 1136: Triangular numbers

  1. Jim Randell 19 December 2016 at 8:34 am

    We can solve this problem directly using the [[ SubstitutedExpression() ]] solver from the enigma.py library without the need to write a program.

    The following run file executes in 88ms.

    #!/usr/bin/env python -m enigma -r
    
    # solver to use
    SubstitutedExpression
    
    # solver parameters
    --answer="(ONE, THREE, SIX, TEN)"
    
    # expressions to solve
    "is_triangular(ONE)"
    "is_triangular(THREE)"
    "is_triangular(SIX)"
    "is_triangular(TEN)"
    

    Solution: ONE = 435, THREE = 17955, SIX = 820, TEN = 153.

  2. Brian Gladman 19 December 2016 at 9:43 am
    from itertools import permutations
    
    # three digit triangular numbers
    t3 = [x * (x + 1) // 2 for x in range(14, 45)]
    
    # pick triangular numbers for ONE and TWO
    for one, six in permutations(t3, 2):
      O, N, E = (int(c) for c in str(one))
      S, I, X = (int(c) for c in str(six))
      
      # check that all their digits are different
      rest = set(range(10)).difference([O, N, E, S, I, X])
      if len(rest) == 4:
        
        # look for the third three digit triangular number
        for T in rest:
          ten = 100 * T + 10 * E + N
          if ten in t3:
            # connsider the two remaining letters
            for H, R in permutations(rest.difference([T]), 2):
              
              # and check for a five digit triangular number
              three = 10000 * T + 1000 * H + 100 * R + 11 * E
              t = 8 * three + 1
              if int(t ** 0.5 + 0.5) ** 2 == t:
                fs = 'ONE = {}, THREE = {}, SIX = {} and TEN = {}.'
                print(fs.format(one, three, six, ten))
    

    This runs in 43ms in a command shell and in 6ms with Python profile.

  3. GeoffR 16 February 2019 at 5:56 pm
    % A Solution in MiniZinc
    include "globals.mzn";
    
    var 1..9:O;   var 0..9:N;   var 0..9:E;
    var 1..9:T;   var 0..9:H;   var 0..9:R;
    var 1..9:S;   var 0..9:I;   var 0..9:X;
    
    constraint all_different ( [O, N, E, T, H, R, S, I, X] );
    
    var 100..999 : ONE = 100*O + 10*N + E;
    var 100..999 : SIX = 100*S + 10*I + X;
    var 100..999 : TEN = 100*T + 10*E + N;
    var 10000..99999 : THREE = 10000*T + 1000*H + 100*R  + 11*E;
    
    set of int: tri3 = { n * (n+1) div 2 | n in 14..44 };
    set of int: tri5 = { n * (n+1) div 2 | n in 141..446 };
    
    constraint ONE in tri3 /\ THREE in tri5 /\ SIX in tri3 /\ TEN in tri3 ;
    
    solve satisfy;
    
    output ["(ONE, THREE, SIX, TEN)" ++ " = " ++ "(" ++ show(ONE) ++ ", " ++ show(THREE)
    ++ ", " ++ show(SIX) ++ ", " ++ show(TEN) ++ ")" ];
    
    % (ONE, THREE, SIX, TEN) = (435, 17955, 820, 153)
    % ----------
    % Finished in 62msec
    
    

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: