Solve problem 91

main
Felix Martin 2021-04-21 15:12:05 -04:00
parent 8c80fdb3c1
commit c281f9e9c0
1 changed files with 47 additions and 24 deletions

View File

@ -1,32 +1,55 @@
from collections import namedtuple
Point = namedtuple("P", ["x", "y"])
Vector = namedtuple("V", ["x", "y"])
def generate_triangles(n):
all_points = [Point(x, y)
for x in range(0, n + 1)
for y in range(0, n + 1)
]
all_points = all_points[1:] # remove (0, 0)
all_pairs = []
for i in range(len(all_points)):
for j in range(i + 1, len(all_points)):
p = (all_points[i], all_points[j])
yield p
def vector(p1, p2):
return Vector(p2.x - p1.x, p2.y - p1.y)
def dot_product(v1, v2):
return v1.x * v2.x + v1.y * v2.y
def has_right_angle(p, q):
o = Point(0, 0)
oq = vector(o, q)
op = vector(o, p)
qp = vector(q, p)
if dot_product(oq, op) == 0:
return True
elif dot_product(oq, qp) == 0:
return True
elif dot_product(op, qp) == 0:
return True
return False
def euler_091():
n = 2
qs = [(x, y)
for x in range(n + 1)
for y in range(n + 1)]
def count_possible_right_triangles(q):
o = (0, 0)
if o == q:
return 0
ps = [(x, y)
for x in range(n + 1)
for y in range(q[0], n + 1)]
for p in ps:
if q == p:
continue
print(o, q, p)
return 0
s = sum(map(count_possible_right_triangles, qs))
return s
count = 0
ts = generate_triangles(50)
for t in ts:
if has_right_angle(*t):
count += 1
return count
if __name__ == "__main__":
solution = euler_091()
print("e091.py: " + str(solution))
assert(solution == 0)
assert(solution == 14234)