Enigmatic Code

Programming Enigma Puzzles

Enigma 1434: Triangular triples

From New Scientist #2595, 17th March 2007

A triangular number is an integer that fits the formula n(n+1)/2; such as 1, 3, 6, 10, 15.

45 is not only a triangular number (9 × 10)/2, but also the product of three different factors (1 × 3 × 15) each of which is itself a triangular number.

But Harry, Tom and I don’t regard that as a satisfactory example since one of those factors is 1; so we have been looking for triangular numbers that are the product of three different factors each of which is a triangular number other than 1.

We have each found a different 3-digit triangular number that provides a solution. Of the factors we have used, one appears only in Harry’s solution and another only in Tom’s solution.

What are the three triangular factors of my solution?

[enigma1434]

Advertisements

2 responses to “Enigma 1434: Triangular triples

  1. Jim Randell 5 May 2013 at 8:37 am

    The generation of the candidate triangular numbers is straightforward. But the code to chose the right three numbers is messier than I’d hoped – especially as it’s easy to find the solution by inspection of the candidates. This Python program runs in 50ms.

    from itertools import count, combinations, permutations
    from collections import defaultdict
    from enigma import diff, multiply, printf
    
    # generate 3-digit triangular numbers that are products
    # of 3 other triangular numbers (not including 1).
    rs = list()
    Ts = list()
    for n in count(2):
      # compute T[n]
      t = n * (n + 1) // 2
      if not(t < 1000): break
    
      if not(t < 100):
        # see if it's the product of three other triangular numbers
        for (t1, t2) in combinations(Ts, 2):
          (t3, r) = divmod(t, t1 * t2)
          if r > 0: continue
          if not(t2 < t3 and t3 in Ts): continue
          # record the result by the factors
          rs.append((t1, t2, t3))
          printf("[T[{n}] = {t} = {t1} x {t2} x {t3}]")
      
      # add it to the list of triangular numbers
      Ts.append(t)
    
    # pick three of the composite triangular numbers
    for fs in combinations(rs, 3):
      # record which numbers the factors appear in
      d = defaultdict(list)
      for (i, f) in enumerate(fs):
        for n in f:
          d[n].append(i)
      # choose two factors that are unique to different numbers
      for (f1, f2) in combinations((k for (k, v) in d.items() if len(v) == 1), 2):
        (h, t) = (d[f1][0], d[f2][0])
        if h == t: continue
        # and the solution is what's left
        fD = diff(fs, (fs[h], fs[t]))[0]
    
        printf("D = {nD} = {fD} [H/T: {nH} = {fH} / {nT} = {fT}]", nD=multiply(fD), nH=multiply(fs[h]), fH=fs[h], nT=multiply(fs[t]), fT=fs[t])
    

    Solution: The three factors in your solution are 3, 6 and 21.

  2. Ahmet Saracoğlu 8 May 2013 at 3:32 pm
    import itertools as t
    
    def tria():
    	trilist=[0]*45
    	counter=0
    	for n in range(2,45):
    		trilist[counter]=n*(n+1)//2
    		counter+=1
    	return trilist
    
    
    def solve():
        lst=tria()
        for indice in t.combinations(lst[0:9],3):
            item1,item2,item3=indice[0],indice[1],indice[2]
            prd=item1*item2*item3
            if prd in lst:
                print(prd,item1,item2,item3)
                
    

    Written in python 3,
    solve()
    378 3 6 21
    990 3 6 55
    630 3 10 21

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: