-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler_039.py
36 lines (32 loc) · 1.17 KB
/
euler_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
34
35
36
# 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?
import math
def exists_triplet(base, perimeter):
for height in range(1,perimeter-base):
hypothenuse = math.sqrt(base**2 + height**2)
if math.modf(hypothenuse)[0] != 0.0:
continue
if base+height+hypothenuse == perimeter:
print("Perimeter: {0}, found sides ({1},{2},{3})".format(perimeter,base,height,int(hypothenuse)))
return True
return False
def count_triplets_for_perimeter(perimeter):
count = 0
for base in range(1,perimeter//3):
if exists_triplet(base, perimeter):
count += 1
return count
max_peri = 0
max_count = 0
# only need to consider even perimeters.
# If it's pythagorean triplet, perimeter will be odd. Proved it on paper.
for i in range(2,1000,2):
c = count_triplets_for_perimeter(i)
if c > max_count:
max_count = c
max_peri = i
print("Largest count is {0} with perimeter {1}".format(max_count,max_peri))