project euler problem 9 https projecteuler netproblem9 special pythagorean triplet a pythagorean triplet is a set of three natural numbers a b c for which a2 b2 c2 for example 32 42 9 16 25 52 there exists exactly one pythagorean triplet for which a b c 1000 find the product abc references https en wikipedia orgwikipythagoreantriple return the product of a b c which are pythagorean triplet that satisfies the following 1 a b c 2 a2 b2 c2 3 a b c n solution36 1620 solution126 66780 solving the two equations a2b2c2 and abcn eliminating c return the product of a b c which are pythagorean triplet that satisfies the following 1 a b c 2 a 2 b 2 c 2 3 a b c n solution 36 1620 solution 126 66780 solving the two equations a 2 b 2 c 2 and a b c n eliminating c
def solution(n: int = 1000) -> int:
    

    product = -1
    candidate = 0
    for a in range(1, n // 3):
        
        b = (n * n - 2 * a * n) // (2 * n - 2 * a)
        c = n - a - b
        if c * c == (a * a + b * b):
            candidate = a * b * c
            if candidate >= product:
                product = candidate
    return product


if __name__ == "__main__":
    print(f"{solution() = }")