Problem statement

A Pythagorean triplet is a set of three natural numbers, $a < b < c$, for which, $$a^2 + b^2 = c^2$$ For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$. There exists exactly one Pythagorean triplet for which $a + b + c = 1000$. Find the product $abc$


This could be easily brute-forced by iterating all pythagorean triples until we find the right one, but I think we can approach this more intelligently.

We have two known formulas that relate $a$, $b$, and $c$: therefore we have a system of simultaneous equations. There are 3 unknowns so we don't have enough information to solve all 3 variables straight away, but this approach will get us part of the way there.

$c$ is the 'unique' variable in this context, as it should be the sum of the other two. It doesn't matter which way around $a$ and $b$ are, they are the same in that respect.

$a + b + c = 1000 \implies c = 1000 - a - b$ $\therefore a^2 + b^2 = (1000 - a - b)^2$ $\implies a^2 + b^2 = a^2 + b^2 + 2ab - 2000a - 2000b + 1\times 10^6$ $\implies 2ab - 2000a - 2000b +1\times 10^6 = 0$ $\implies ab - 1000a - 1000b + 500\times 10^3 = 0$ $\implies a(b - 1000) = 1000(b - 500)$ $\implies a = \frac{1000(b - 500)}{b - 1000}$

We have expressed $a$ in terms of $b$ for all values of $a$ and $b$ such that $c = \sqrt{a^2 + b^2}$ and $a + b + c = 1000$. We can plot $a$ against $b$:

Plot of a against b

Whilst it is true that all values of $a$ resulting from any value of $b$ will satisfy our starting equations, we are told there is only one solutions where $a, b, c$ are all integers greater than zero.

Unfortunately, finding integer solutions is non-trivial. This is a Diophantine equation, and a non-linear one at that. It is beyond my current mathematical ability to solve (I'm not sure if there is even a method to solve it).

I am, therefore, going to turn to a brute-force approach for solving this equation. We can easily see from the above plot that the upper bound for $b$ is 500 (above which $a$ is negative), so that will be the upper bound for my search.


Test every positive integer value of $b$ in the above equation until we find one that yields an integer value for $a$ also. We are told there is only one integer solution, so I will exit upon first finding one.

#include <math.h>
#include <stdio.h>
int main()
	double a = 0;
	double b = 1;

	/* Iterate values of b looking for integer result a */
	for (b = 1; b < 500; b++) {
		a = (1000 * (b - 500)) / (b - 1000);

		/* Break if a is an integer */
		if (floor(a) == a) {

	/* Compute c from a^2 + b^2 = c^2 */
	double c = sqrt((pow(a, 2) + pow(b, 2)));

	printf("%f", a * b * c);