mu chance or much chance ?

日々の戯れ言

Project Euler 39

  • 問題

Problem 39:Integer right triangles
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?

  • 解答例
limit = 1000
maxId = 0
maxCount = 0

for p in range(2, limit + 1, 2):
    count = 0
    for a in range(2, p // 3 + 1):
        if (p * (p - 2 * a)) % (2 * (p - a)) == 0:
            b = (p * (p - 2 * a)) // (2 * (p - a))
            c = p - a - b
            if a <= b and b <= c:
                count += 1
    if maxCount < count:
        maxCount = count
        maxId = p

print(maxId, maxCount)