Find Pythagorean triplet for which a b c 1000
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The problem asks for a Pythagorean triplet a, b, and c such that a^2 + b^2 = c^2 and a + b + c = 1000. This is a classic Project Euler style problem because it rewards both brute-force thinking and mathematical simplification. You can solve it by search, but number theory gives a faster route and explains why the answer exists.
Brute Force Search
The direct approach is to try values until the equations match. Since a < b < c for a positive Pythagorean triplet, we can cut down the search space immediately.
This works because c is determined by the target sum, so you only need to loop over a and b.
Even though the brute-force method is simple, it still does more work than necessary.
Use Euclid’s Formula
Primitive Pythagorean triplets can be generated with:
a = m^2 - n^2
b = 2mn
c = m^2 + n^2
where m > n > 0, m and n are coprime, and one is even.
Now apply the sum condition:
a + b + c = 1000
Substitute the formulas:
m^2 - n^2 + 2mn + m^2 + n^2 = 1000
This simplifies to:
2m^2 + 2mn = 1000
or:
m(m + n) = 500
That is much easier to search than all possible a, b, c triples.
Faster Mathematical Search
We can search over m and n instead.
This returns the same triple much faster.
The Actual Answer
The unique positive triplet for sum 1000 is:
a = 200
b = 375
c = 425
Check it:
200^2 + 375^2 = 40000 + 140625 = 180625
425^2 = 180625
and:
200 + 375 + 425 = 1000
If you also need the product:
That gives 31875000.
Why the Euclid Method Is Better
The brute-force method is fine for a one-off puzzle, but the Euclid-based method scales better and explains the structure of the solution. It turns a numeric search into a factored constraint on m and n, which is easier to reason about mathematically.
That matters if:
- the target sum changes
- you need to solve many similar queries
- you want to prove properties of the solution
In those cases, formula-driven search is the better foundation.
A More Efficient Brute Force Variant
If you still want a simple search, reduce the loops by using the sum directly.
This is still brute force, but it avoids a third nested loop and uses sensible upper bounds.
Common Pitfalls
The most common mistake is running three nested loops over a, b, and c. Since c is determined by a + b + c = 1000, that extra loop is unnecessary.
Another issue is forgetting to enforce a < b < c, which multiplies duplicate work.
Developers also sometimes use Euclid’s formula without accounting for scaling. The sum 1000 solution is not primitive, so the final triplet comes from multiplying a smaller primitive one.
Summary
- The target triple satisfies both the Pythagorean equation and the fixed-sum equation.
- A direct search works, but you only need to loop over
aandb. - Euclid’s formula gives a faster and more insightful solution.
- The triplet is
200,375,425. - The product of the triplet is
31875000.

