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?



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:


  5. Ahmet Saracoğlu 5 July 2013 at 9:56 am
    import time
    import itertools
    def Solve():
        for i in range(len(pList)):
            if ((c-a2b2)*(c+a2b2)*e*d)==((f-d2e2)*(f+d2e2)*a*b):
                if abs(a-b)<c<(a+b):
    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
    import itertools
    def Solve():
        for i in range(len(pList)):
            for j in range(len(sqrs)):
                if c not in (a,b,d,e):
                    if abs(a-b)<c<(a+b):
                        for k in range(len(sqrs)):
                            if c2==(sqrs[k]-d2e2)*ab:

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: