Consider a right triangle whose side length is a triplet of {a, b, c} and an integer, and let p be the length around it. When p = 120, there are three solutions:
{20,48,52}, {24,45,51}, {30,40,50}
What is the maximum number of solutions when p ≤ 1000? http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2039
For each p, I wrote the code to check if ʻa ^ 2 + b ^ 2is equal to
(p-a-b) ^ 2` while changing a and b. I wrote it in while somehow, but I think it's better to write it in for.
def main():
MAX = 1000
sq = [x**2 for x in range(MAX)]
p = MAX-1
d_max = 0
ans = 0
while p > 0:
(a, b) = (p//2,p//2)
d = 0
while a > 0:
if sq[a]+sq[b] == sq[p-a-b]:
d+=1
elif sq[a]+sq[b]< sq[p-a-b]:
break
while b > 0:
if sq[a]+sq[b] == sq[p-a-b]:
d += 1
elif sq[a]+sq[b] < sq[p-a-b]:
break
b -=1
a, b = a-1, a-1
if d > d_max:
ans, d_max = p, d
print ans, d_max
p -= 1
print ans
main()
Recommended Posts