Python Implementation of Problem 39

View source code here on GitHub!

Problem Solution

Project Euler Problem 39

Problem:

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

python.src.p0039.main() int
 1"""
 2Project Euler Problem 39
 3
 4Problem:
 5
 6If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions
 7for p = 120.
 8
 9{20,48,52}, {24,45,51}, {30,40,50}
10
11For which value of p ≤ 1000, is the number of solutions maximised?
12"""
13
14
15def main() -> int:
16    biggest = 0
17    biggest_size = 0
18    for p in range(3, 1000):
19        solutions = set()
20        for c in range(3, p):
21            for b in range(1, (p - c) // 2 + 1):
22                a = p - b - c
23                if a > c or b > c:
24                    continue
25                elif a*a + b*b == c*c:
26                    solutions.add(frozenset((a, b, c)))
27        num_solutions = len(solutions)
28        if num_solutions > biggest_size:
29            biggest_size = num_solutions
30            biggest = p
31    return biggest

Tags: geometry, partition, pythagorean-triple