<jupyter_start><jupyter_text>Problem 39 Integer right trianglesIf $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 \le 1000$, is the number of solutions maximised? Solution<jupyter_code>from math import isqrt, sqrt
def compute(n: int) -> int:
    perimeters = {}
    for a in range(3, n // 3):
        for b in range(a, n // 2):
            k = a * a + b * b
            if sqrt(k) % 1 == 0:
                p = a + b + isqrt(k)
                if p in perimeters:
                    perimeters[p] += 1
                else:
                    perimeters[p] = 1
    return max(perimeters, key=perimeters.get)
compute(1_000)
%timeit -n 100 -r 1 -p 6 compute(1_000)<jupyter_output>24.1138 ms ± 0 ns per loop (mean ± std. dev. of 1 run, 100 loops each)
StarcoderdataJupyterStructuredCleanDedup