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?

  • 1
    $\begingroup$ I personally have memorized most of the small Pythagorean triples (this one, for example, is 8, 15, 17). $\endgroup$ – Joe Z. Dec 13 '12 at 20:36
  • 2
    $\begingroup$ tbh, I bet they were testing your ability to admit that you didn't have the tools to solve it right there in the limited amount of time. I highly doubt they wanted you to know the answer. $\endgroup$ – picakhu Dec 13 '12 at 20:41
  • $\begingroup$ It certainly helps to know the formula for general relatively prime triples, since you can solve for any triple such that $a+b+c$ divides 40 and then multiply to get a triple with $a+b+c=40$. $\endgroup$ – Thomas Andrews Dec 13 '12 at 20:41

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


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

  • 1
    $\begingroup$ Or you can rewrite that as $ab=40a+40b-800$ or $(a-40)(b-40)=800$. Given that you know $0<a,b<40$, this shouldn't be too hard to work out. $\endgroup$ – Thomas Andrews Dec 13 '12 at 20:46
  • $\begingroup$ @thomas, you know better, you know 0<a,b<20 $\endgroup$ – picakhu Dec 13 '12 at 20:48
  • $\begingroup$ @Thomas: Yes, that should work too. Still, at some point you will have to guess $a$ and $b$ I think. $\endgroup$ – TMM Dec 13 '12 at 20:55
  • $\begingroup$ This is exactly the sort of thing I was looking for. Thank you! $\endgroup$ – NPE Dec 14 '12 at 7:09
  • $\begingroup$ I took the spirit of the question to be about integers, i.e., allowing negatives, 5 years later be damned $\endgroup$ – AmateurMathPirate Sep 3 '17 at 15:25

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.)


Neat question. If one were to allow for answers in $\mathbb{Z}$, you can have as many as 18 solutions. $$\begin{align} &(65,72,-97) &&(90,56,-106) &&&(140,48, -148) \\ &(240,44,-244) &&(440, 42, -442) &&&(840, 41, -841) \\ &(45,200,-205) &&(50,120,-130) &&&(60,80,-100) \end{align}$$

$$\begin{align} &***(8,15,17)*** &&(24,-10, 26) &&&(32,-60,68) \\ &(36,-160,164) &&(38,-360,362) &&&(39,-760,761) \\ &(-120,35,125) &&(-40,30,50) &&&(0,20,20) \end{align}$$

Solution: $$\begin{cases} a^2+b^2=c^2 &&&&&&(1)\\a+b+c=40 &&&&&&(2)\end{cases}$$

$$(2) \ \text{for c into} \ (1)\to $$ $$\begin{align} a^2+b^2&=[40-(a+b)]^2 &&\implies \\ a^2+b^2&=1600-80(a+b)+(a+b)^2 &&\implies \\ 0&=1600-80(a+b)+2ab &&\implies \\ 0&=ab-40(a+b)+800 &&\implies \\ 800 &= ab - 40(a+b) + 1600 &&\implies \\ 800 = d_1 \cdot d_2&= (a-40)(b-40)&&\implies \\ &\Bigg{\Downarrow} \\ \end{align}$$

$$\begin{cases} a=40+d_1 \\ b=40+d_2 \\ c =-40-(d_1+d_2)\end{cases} $$

The sides of the triangle are now defined parametrically in terms of an arbitrary choice of divisors of $800$. Here, for any choice of a divisor-pair $(d_1,d_2)$, switching the order doesn't produce a new solution, (other times it can), but there is an additional $(-d_1,-d_2)$ solution.

Since, $$\begin{align} 800=2^{\color{red}{5}} \cdot 5^{\color{red}{2}} &\implies (\color{red}{5}+1)(\color{red}{2}+1)=18 \ \text{divisors} \\ &\implies 9 \ \text{divisor-pairs} \\ &\implies 18 \ \text{divisor-pairs including negatives} \end{align}$$

we expect $18$ solutions overall, evaluated above.


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


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


$$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$$


$$(\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.


In practical, I will first solve this problem by assuming a=b. This gives approximately (11.72,11.72,16.56), then I try all possible ways around this solution. I can first assume the largest side is 17, then try (12,11),(13,10),(14,9),(15,8).


Here's a non-algebraic idea. Loosen your tie until you can remove it over your head. Then adjust it to 20 cm long when stretched out (ask the interview panel, politely, to provide a ruler) ie 40cm total loop length. Now form this into a right-angled triangle (ask for some help from the interview panel and use of their table - politely) and vary the legs until both are integer length (in cm). The hypotenuse must be integer (40 minus leg lengths) and being right-angled Pythagoras does the rest. My hunch is that this is the sort of approach they'd be after given the way the info. is provided ie a blend of background knowledge and practical application (based on many job interviews and enjoying 'watching the watchers' whilst going through the motions).

  • 1
    $\begingroup$ I tried your method. The problem is that the lengths are too small and the tie is not rigid enough so you might miss the correct answer. In my case, I tried successive lengths of one of the right angle sides (5,6,7,8) but I got 8,16,17 instead of 8,15,17 (which is the correct answer) so this doest help. However, a better way to do this would be if you have a rigid string, 3 pins, and a pin board. You could then create a 40cm loop and try out successive integer lengths of one of the sides of the right-angle triangle, but not likely possible during an interview :) $\endgroup$ – baibo Dec 14 '18 at 19:07

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...

  • $\begingroup$ The solution you give is not integer. $\endgroup$ – Martin Argerami Dec 13 '12 at 22:56
  • 5
    $\begingroup$ The term "Pythagorean triple" is used only for integer solutions to $a^2+b^2=c^2$ $\endgroup$ – Thomas Andrews Dec 14 '12 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.