Finding a Pythagorean triple $a^2 + b^2 = c^2$ with $a+b+c=40$

1.8k Views Asked by At

Let's say you're asked to find a Pythagorean triple $a^2 + b^2 = c^2$ such that $a + b + c = 40$. The catch is that the question is asked at a job interview, and you weren't expecting questions about Pythagorean triples.

It is trivial to look up the answer. It is also trivial to write a computer program that would find the answer. There is also plenty of material written about the properties of Pythagorean triples and methods for generating them. However, none of this would be of any help during a job interview.

How would you solve this in an interview situation?

4

There are 4 best solutions below

5
On BEST ANSWER

Assuming you do have a pen and paper, you could substitute $c = 40 - a - b$ into the first equation to get

$$a^2 + b^2 = (40 - a - b)^2 = a^2 + b^2 + 1600 - 80(a + b) + 2ab.$$

Rewriting this equation, you get

$$a + b - 20 = \frac{ab}{40}.$$

From this it follows that $ab$ has to be a multiple of $40$, i.e., one of them is a multiple of $5$. That narrows it down to only a few options...


If that's still too much brute-force, you could also note that $a + b > 20$ from the above equation, and $a + b < 27$, since $c$ has to be the largest of the three. This leaves only the three pairs

$$\{(5,16),(10,16),(15,8)\}.$$

Looking at the earlier equation, you see the third pair is the right one.

0
On

The general pythagorean triple can be written (up to swapping $a$ and $b$) as $$a=2kuv$$ $$b=k(u^2-v^2)$$ $$ c=k(u^2+v^2)$$ where $k,u,v$ are positive integers, $u,v$ are relatively prime with different parity and $u\geq v$. For $a+b+c=40$, then, you get the condition $2k(u^2+uv)=40$, so you need $u^2+uv=u(u+v)$ to be a factor of $20$. Since $u,v$ have different parity, and they are positive, you know $u+v>1$ is odd, so $u+v=5$.

Given that $u\geq v$, that yields $u=4,v=1$ and $u=3,v=2$. But $u=3$ isn't possible, since $3$ is not a factor of $20$. So The only solution is $(u,v)=(4,1)$ and therefore the only solution is $k(15,8,17)$ which you can see must have $k=1$, and you are done - the only solution is $(15,8,17)$. And $(8,15,17)$, if you count that as different.

For the more general problem, $a+b+c=2n$ (the sum of a Pythagorean triple is always even) this amounts to factoring $n=kuw$ with the following conditions:

  • $k,u,w$ are positive in
  • $w$ is odd
  • $u<w<2u$

Given such a solution, you get a triple (by setting $v=w-u$:) $$a=2ku(w-u)$$ $$b=kw(2u-w)$$ $$c=k(2u^2+w^2-2uw)$$

And this gives all such triples (modulo swapping $a$ and $b$.)

Listing these amounts to first listing the set of possible values of $w$, which can be any odd factors of $n$ such that $1<w<\sqrt{2n}$. Then find values of $u$ with $w/2<u<w$ and $uw|n$. Then set $k=n/uw$ and you have your triple $(k,u,w)$ from which you can compute an $(a,b,c)$.

(If you want to allow $ab=0$, the change the above to $u\leq w < 2u$. Then $u=v=1$ and $k=n$ is always a solution.)

2
On

This is a rect triangle with a length of 40. for example triangle (3,4,5) with a length of 12, you can get the solution: (120/12,160/12,200/12). the point is that you know some patterns in advanced. (5,12,13) etc...

0
On

$$a^2=(c-b)(c+b) \Rightarrow b+c = \frac{a^2}{c-b}$$

$$a+\frac{a^2}{c-b}=40$$

For simplicity let $c-b=\alpha$.

then

$$a^2+\alpha a-40\alpha =0$$ Since this equation has integral solutions,

$$\Delta=\alpha^2+160 \alpha$$

is a perfect square.Thus

$$\alpha^2+160 \alpha =\beta^2$$

Or

$$(\alpha+80)^2=\beta^2+80^2 \,.$$

This way we reduced the problem to finding all pytagorean triples of the type $(80, ??, ??)$. This can be easily done if you know the formula, or by setting

$$80^2=(\alpha+80-\beta)(\alpha+80+\beta)$$ and solving.