Project Euler Problem 039

Statement

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?

Solution

A brute-force solution solves it in less than a minute in Python:

def find_combinations_of_perimeter(per):
    result = 0
    a = 1
    b = 1
    c = 1
    for a in range(1, per // 2):
        for b in range(a, per // 2):
            c = per - b - a
            if (a ** 2 + b ** 2) == c ** 2:
                result += 1
    return result
 
if __name__ == '__main__':
    max_count = 0
    for i in range(12, 1000):
        c = find_combinations_of_perimeter(i)
        if c > max_count:
            max_count = c
            result = i
    print("The result is:", result)

The Python file is available for download here.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License