-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path039.py
33 lines (29 loc) · 976 Bytes
/
039.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Project Euler - Problem 39
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?
"""
maxSol=0
maxPer=0
for perimeter in range(3,1001):
solutions=[]
for a in range(1,perimeter/4+1):
for b in range(perimeter/4-1,perimeter/2):
c=perimeter-a-b
if c*c==a*a+b*b:
solutions.append((a,b,c))
if len(solutions) > maxSol:
maxSol=len(solutions)
maxPer=perimeter
print "Found a maximum for perimeter of %d" % maxPer
print "Maximum solutions is %d for a perimeter of %d" % (maxSol, maxPer)
# output:
# Found a maximum for perimeter of 12
# Found a maximum for perimeter of 60
# Found a maximum for perimeter of 120
# Found a maximum for perimeter of 240
# Found a maximum for perimeter of 720
# Found a maximum for perimeter of 840
# Maximum solutions is 7 for a perimeter of 840