Enigmatic Code

Programming Enigma Puzzles

Enigma 1756: Two triangles

From New Scientist #2924, 6th July 2013 [link]

I have drawn two triangles. The lengths of their sides are six different single-digit integers. The triangles have one angle in common.

What are the lengths of the sides of the two triangles?

[enigma1756]

Advertisements

6 responses to “Enigma 1756: Two triangles

  1. Jim Randell 3 July 2013 at 6:30 pm

    The following Python program uses several of the built-in libraries. It runs in 57ms.

    from collections import defaultdict
    from itertools import combinations
    from fractions import Fraction as F
    from enigma import irange, printf
    
    # possible sides
    sides = list(irange(1, 9))
    
    # use the cosine rule to calculate angles
    cosine = lambda a, b, c: F(b * b + c * c - a * a, 2 * b * c)
    
    # consider possible triangles
    r = defaultdict(set)
    for (a, b, c) in combinations(sides, 3):
      if a + b > c:
        # calculate the cosines of the angles
        angles = (cosine(a, b, c), cosine(b, a, c), cosine(c, a, b))
        # record triangles by angles
        for x in angles: r[x].add((a, b, c))
    
    for (k, v) in r.items():
      # find pairs of triangles that share an angle
      for (X, Y) in combinations(v, 2):
        # but not sides
        if set(X).intersection(Y): continue
        printf("X={X} Y={Y} angle=acos({k})")
    

    Solution: The length of sides in the two triangles are 2, 3, 4 and 6, 7, 8.

  2. Brian Gladman 3 July 2013 at 7:26 pm
    from itertools import combinations, product
    from collections import defaultdict
    from fractions import Fraction
    
    d = defaultdict(list)
    # all combinations of three different sides
    for a, b, c in combinations(range(1, 10), 3):
      # skip impossible triangles
      if a + b > c:
        # form the three angles as fractions
        f1 = Fraction(b ** 2 + c ** 2 - a ** 2, 2 * b * c)
        f2 = Fraction(c ** 2 + a ** 2 - b ** 2, 2 * c * a)
        f3 = Fraction(a ** 2 + b ** 2 - c ** 2, 2 * a * b)
        # record the sides indexed on these angles
        d[frozenset((f1, f2, f3))] += [(a, b, c)]
    
    # for combinations of two angle triples
    for s1, s2 in combinations(d, 2):
      # if they share one angle in common
      if len(s1 & s2) == 1:
        # then find disjoint sets of side triples
        for t1, t2 in product(d[s1], d[s2]):
          if not set(t1) & set(t2):
            print(t1, t2)
    
  3. arthurvause 3 July 2013 at 9:33 pm

    Pretty much the same solution as Jim and Brian:

    from itertools import combinations as comb, permutations as perm
    from fractions import Fraction
    
    def cosines(t): # return the cosines of a triangle
      return set([Fraction(a*a+b*b-c*c,2*a*b) for a,b,c in perm(t)])
    
    candidates = [(set(x),cosines(x))
                  for x in comb(range(1,10),3)
                  if 2*max(x) < sum(x)]
    
    for t1,t2 in comb(candidates,2):
      if len(t1[1] & t2[1])==1 and t1[0]&t2[0]==set():
        print sorted(t1[0]),sorted(t2[0])
    
  4. ahmet çetinbudaklar 4 July 2013 at 12:56 pm

    CosA=(a^2+b^2-c^2)/(2ab)=(d^2+e^2-f^2)/(2de) gives us (ab)/(de)=(a^2+b^2-c^2)/(d^2+e^2-f^2) from which we can try the following triangle side lengthsto reach the unique solution by trial and error:

    (2,3,4),(2,4,5),(2,5,6),(2,6,7),(2,7,8),(2,8,9),
    (3,4,5),(3,4,6),(3,5,6),(3,5,7),(3,6,7),(3,6,8),(3,7,8),(3,7,9),(3,8,9),
    (4,5,6),(4,5,7),(4,5,8),(4,6,7),(4,6,8),(4,6,9),(4,7,8),(4,7,9),(4,8,9),
    (5,6,7),(5,6,8),(5,6,9),(5,7,8),(5,7,9),(5,8,9),
    (6,7,8),(6,7,9),(6,8,9),
    (7,8,9)

  5. Ahmet Saracoğlu 5 July 2013 at 9:56 am
    import time
    print(time.time())
    import itertools
    
    pList=list(itertools.permutations(range(1,10),6))
    
    def Solve():
        for i in range(len(pList)):
            a,b,c,d,e,f=pList[i][0],pList[i][1],pList[i][2],pList[i][3],pList[i][4],pList[i][5]
            a2b2=((a**2)+(b**2))**0.5
            d2e2=((d**2)+(e**2))**0.5
            if ((c-a2b2)*(c+a2b2)*e*d)==((f-d2e2)*(f+d2e2)*a*b):
                if abs(a-b)<c<(a+b):
                    print(a,b,c,d,e,f)
                    return
                                          
    
    Solve()
    print (time.time())
    
  6. Ahmet Saracoğlu 5 July 2013 at 10:57 am

    This version is faster than my previous code,

    import time
    import math
    print(time.time())
    import itertools
    
    sqrs=[1,4,9,16,25,36,49,64,81]
    pList=list(itertools.permutations(range(1,10),4))
    
    
    def Solve():
        for i in range(len(pList)):
            a,b,d,e=pList[i][0],pList[i][1],pList[i][2],pList[i][3]
            a2b2,d2e2=a**2+b**2,d**2+e**2
            ed,ab=e*d,a*b
    
            for j in range(len(sqrs)):
                c=j+1
                if c not in (a,b,d,e):
                    c2=(sqrs[j]-a2b2)*ed
                    if abs(a-b)<c<(a+b):
                        for k in range(len(sqrs)):
                            if c2==(sqrs[k]-d2e2)*ab:
                                print(a,b,c,d,e,k+1)
                                return
    
    Solve()
    print(time.time())
    

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: