forked from MalayAgr/Project-Euler-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteger right triangles.py
46 lines (33 loc) · 1.08 KB
/
Integer right triangles.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
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
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?
'''
pValues = map(lambda x: x, range(121, 1000))
def getNumberOfSolutions(p):
solutionCount = 0
pSquare = p**2
l = []
for a in range(1, p - 1):
b = int(((2*p*a) - pSquare)/(2*(a - p)))
if b != 0:
c = (a**2 + b**2)**(1/2)
if a + b + c == p and c <= (a + b):
triangle = [a, b, c]
triangle.sort()
if triangle not in l:
solutionCount += 1
l.append(triangle)
return solutionCount
def main():
maxSolutions = 3
requiredValue = 0
for p in pValues:
solutionCount = getNumberOfSolutions(p)
if solutionCount > maxSolutions:
maxSolutions = solutionCount
requiredValue = p
print(requiredValue, maxSolutions)
if __name__ == '__main__':
main()