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]

Advertisements

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

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: