Integer triangles sharing angles

Here’s another brute-force number theory puzzle, in the spirit of the Russian puzzle I looked at a couple weeks ago. From what would Martin Gardner tweet: “Two triangles share one angle but have six different side lengths (all whole numbers between 1 and 9). Find those lengths.”

Do we expect such triangles to exist? Well, clearly there are lots of different-sized right triangles, which share an angle. To take the smallest possible example, 3-4-5 and 6-8-10 are both the sides of right triangles, and they share an angle. But sadly, 10 is too large…

This is a bit tricky to think about, because the angles of an integer-sided triangle aren’t going to be anything nice. So instead of looking at the angles themselves let’s look at their cosines. Given a triangle with sides having lengths (a, b, c), the angle C, opposite the side of length c satisfies

c^2 = a^2 + b^2 - 2ab \cos C

which we can rearrange to give

\cos C = {a^2 + b^2 - c^2 \over 2ab}

This is just the law of cosines from “high school trigonometry”, which is a generalization of the Pythagorean theorem to non-right-angled triangles.

So now the search for a solution is just bookkeeping. Let’s look at all the possible triplets (a, b, c) of distinct integers that could be the sides of a triangle. For the sake of keeping things canonical, let’s force a > b > c. The triangle inequality says that a > b+c, or c > a-b, so we get a > b > c > a-b > 0. Furthermore we can see that a must be at least 4, since each of those four inequalities is strict. So we loop over all those possible triples and store them in a Python dictionary according to the cosines of their angles. The following code will ask for an upper bound on the side lengths and carry out the search.


#Import some useful data structures.
from collections import defaultdict
from sets import Set

#Greatest common divisor (by the Euclidean algorithm).  For reducing fractions.
def gcd(a,b):
    while b:
        a, b = b, a%b
    return a

#Takes as input the three sides of a triangle;
#Outputs red_num, red_den such that red_num/red_den is the cosine of the angle opposite the first side given
#as a fraction in lowest terms
def cosine_law(c, a, b):
    raw_num = (-c**2 + a**2 + b**2)
    raw_den = (2*a*b)
    g = gcd(raw_num, raw_den)
    red_num = raw_num/g
    red_den = raw_den/g
    return red_num, red_den

out = defaultdict(set)

maxn = int(raw_input("Largest side to allow? ")) + 1

#Loop over possible triples a, b, c with a > b > c > a-b
#(i. e. in decreasing order and satisfying the triangle inequality)
#and add the cosines of their angles to the dictionary out
for a in range(4, maxn):
    for b in range(-int(-a/2), a):
        for c in range(a-b+1, b):
            if gcd(gcd(a,b),c) == 1:
                cosA = cosine_law(a,b,c)
                cosB = cosine_law(b,a,c)
                cosC = cosine_law(c,a,b)
                #store with vertex first, then larger of two second
                out[cosA].add((a,b,c))
                out[cosB].add((b,a,c))
                out[cosC].add((c,a,b))

#Look through out for pairs of triangles which share an angle
#and have all three sides different.
numsols = 0
for key in out:
    if len(out[key]) >= 2:

       triangles = list(out[key])
       for i in triangles:
           for j in triangles:
               sides = Set([i[0], i[1], i[2], j[0], j[1], j[2]])
               if len(sides) == 6 and i[0] >= j[0]:
                   #print key, i, j
                   numsols = numsols+1

And if we input 9 when it asks for that largest side the output is precisely

(11, 16) (6, 8, 7) (3, 4, 2)

The 2-tuple is the rational number 11/16, the cosine of the angle that these triangles have in common. The triplets represent triangles with an angle cos^{-1} 11/16, with the side opposite that angle listed first. So the angle opposite the side of length 6 in a 6-7-8 triangle and the angle opposite the side of length 3 in a 2-3-4 triangle both have cosine $11/16$; they are both angles of $\cos^{-1} 11/16$, or about 46.6 degrees.

This leads naturally to the question: are such pairs of triangles rare? The answer is “not at all”. There are 30 solutions to this problem with all sides under 15, 951 with all sides under 30, and 81,014 with all sides under 100.

In fact, heuristically, we expect lots of such solutions. The number of different integer triangles with all their sides less than n is on the order of n3. From the law of cosines, the angles of these triangle will have cosines that are rational numbers with numerator and denominator on the order of n2; there are n4 such different numbers. We can wave our hands and say the magic words birthday problem; since the number of possible cosines is much greater than the square root of the number of angles we need to find cosines for, if everything happens “at random” there ought to be lots of collisions — and indeed there are.

21 thoughts on “Integer triangles sharing angles

  1. I am really loving the theme/design of your
    website. Do you ever run into any web browser compatibility
    issues? A number of my blog readers have complained about my website not working correctly in Explorer but
    looks great in Chrome. Do you have any advice to help fix this
    problem?

  2. The most recent style in earning college credits and actually a college degree is utilizing the net of getting
    classes immediately on-line with a school or university,
    and the power. To remain competitive most universities and colleges now offer degrees,
    and for students of all ages to earn school credit, and less costly the best college courses on-line to make it simple, convenient.
    You’ll find earning a college degree on the internet or using bestcollegecourses online is quite popular.
    Any specified college’s total registration can reach as much as
    fifty per cent online. On recent years the increase of
    on-line best college courses and degrees has increased immensely and
    continues to develop in numbers.

  3. Appreciating the hard work you put into your site and detailed information you offer.
    It’s awesome to come across a blog every once in a while that isn’t the same outdated rehashed material.
    Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  4. hello!,I like your writing so so much! percentage we communicate more about your article on AOL?
    I require an expert in this space to resolve my problem.
    Maybe that is you! Looking ahead to see you.

  5. Magnificent items from you, man. I’ve remember your stuff previous to and you’re just
    too wonderful. I really like what you’ve bought here, really
    like what you’re saying and the way in which you are saying
    it. You make it entertaining and you still care for to stay it smart.
    I cant wait to learn far more from you. That is actually a tremendous
    website.

  6. This part contains numbers of tourism attractions and can be easily covered by the car.

    Vehicles and especially brand new ones are quite expensive,
    thus many people need to work hard until they earn enough money for purchasing a
    vehicle. When choosing a vehicle online it is recommended meeting the seller and inspecting the auto.

  7. Pretty great post. I simply stumbled upon your blog and wished to say
    that I’ve truly enjoyed surfing around your weblog posts.
    In any case I’ll be subscribing to your rss feed and I’m hoping you write
    again very soon!

  8. You really make it appear really easy along with your presentation however
    I find this matter to be actually something which I feel I’d never understand.

    It kind of feels too complicated and extremely broad for me.
    I am looking ahead in your subsequent put up, I will try to get the cling of it!

  9. Hello, i think that i noticed you visited my weblog so i came to return the want?.I am trying to to find issues to
    enhance my web site!I suppose its adequate to make use of
    a few of your concepts!!

  10. Great goods from you, man. I’ve understand your stuff previous to and you’re just too great.
    I really like what you’ve acquired here, certainly
    like what you are stating and the way in which you say it.

    You make it entertaining and you still care for to keep it smart.

    I cant wait to read much more from you. This is really a wonderful web site.

  11. Just want to say your article is as surprising.
    The clearness to your post is simply great and i can suppose you’re an expert on this subject.
    Well with your permission allow me to grasp your RSS feed to keep
    up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

Leave a Reply

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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s