代码拉取完成,页面将自动刷新
"""
Problem 91: https://projecteuler.net/problem=91
The points P (x1, y1) and Q (x2, y2) are plotted at integer coordinates and
are joined to the origin, O(0,0), to form ΔOPQ.
There are exactly fourteen triangles containing a right angle that can be formed
when each coordinate lies between 0 and 2 inclusive; that is,
0 ≤ x1, y1, x2, y2 ≤ 2.
Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed?
"""
def solution(limit: int = 50) -> int:
"""
assume x1 <= x2
>>> print(solution(1))
3
>>> print(solution(2))
14
"""
res = 0
for x1 in range(limit+1):
for y1 in range(limit+1):
for x2 in range(x1, limit+1):
for y2 in range(limit+1):
if x1*y2 == x2*y1:
continue
else:
dx = x2-x1
dy = y2-y1
if any([x1*x2+y1*y2 == 0, x1*dx+y1*dy == 0, x2*dx+y2*dy == 0]):
if x1==y2:
if y1<=y2:
# print((x1, y1), (x2, y2))
res+=1
else:
# print((x1, y1), (x2, y2))
res+=1
return res
if __name__ == "__main__":
from doctest import testmod
testmod()
print(solution())
# 14126
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。