-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patheuler009.py
More file actions
49 lines (36 loc) · 1.16 KB
/
euler009.py
File metadata and controls
49 lines (36 loc) · 1.16 KB
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
47
48
49
#!/bin/python3
import sys
class PythagoreanTriplet(object):
def __init__(self, n):
self.n = n
def maximum_abc(self):
m, n = self.find_triplet()
# print("m is:", m)
# print("n is:", n)
if(m):
return self.product_of_abc(m, n)
else:
return -1
def find_triplet(self):
for m in range(self.n, 0, -1):
n = self.compute_n(m)
# print("trying to find m is:", m)
# print("trying to find n is:", n)
if(n.is_integer() and m > n and n > 0):
return m, int(n)
return False, False
def compute_n(self, m):
return (self.n - 2 * m ** 2) / (2 * m)
def product_of_abc(self, m, n):
return self.compute_a(m, n) * self.compute_b(m,
n) * self.compute_c(m, n)
def compute_a(self, m, n):
return m ** 2 - n ** 2
def compute_b(self, m, n):
return 2 * m * n
def compute_c(self, m, n):
return m ** 2 + n ** 2
t = int(input().strip())
for a0 in range(t):
n = int(input().strip())
print(PythagoreanTriplet(n).maximum_abc())