Pythagorean triplet
mathematical problem
a + b + c = 1000
number theory
special triplet

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.

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.

python
1def find_triplet_with_sum(total: int):
2    for a in range(1, total):
3        for b in range(a + 1, total - a):
4            c = total - a - b
5            if a * a + b * b == c * c:
6                return a, b, c
7    return None
8
9print(find_triplet_with_sum(1000))

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.

We can search over m and n instead.

python
1def euclid_triplet_with_sum(total: int):
2    for m in range(2, int(total ** 0.5) + 1):
3        for n in range(1, m):
4            a = m * m - n * n
5            b = 2 * m * n
6            c = m * m + n * n
7            s = a + b + c
8
9            if total % s == 0:
10                k = total // s
11                return k * a, k * b, k * c
12    return None
13
14print(euclid_triplet_with_sum(1000))

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:

python
a, b, c = 200, 375, 425
print(a * b * c)

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.

python
1def fast_bruteforce(total: int):
2    for a in range(1, total // 3):
3        for b in range(a + 1, total // 2):
4            c = total - a - b
5            if a * a + b * b == c * c:
6                return a, b, c
7    return None
8
9print(fast_bruteforce(1000))

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 a and b.
  • Euclid’s formula gives a faster and more insightful solution.
  • The triplet is 200, 375, 425.
  • The product of the triplet is 31875000.

Course illustration
Course illustration

All Rights Reserved.