1 Star 0 Fork 0

阿坚/projecteuler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
p091.py 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
阿坚 提交于 2022-06-18 16:04 . 91,92,93,done
"""
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kuperain/projecteuler.git
git@gitee.com:kuperain/projecteuler.git
kuperain
projecteuler
projecteuler
master

搜索帮助