1 Star 0 Fork 0

阿坚/projecteuler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
p075.py 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
阿坚 提交于 2022-05-30 21:44 . 76-undone
"""
Problem 75: https://projecteuler.net/problem=75
It turns out that 12 cm is the smallest length of wire that can be bent to form an
integer sided right angle triangle in exactly one way, but there are many more examples.
12 cm: (3,4,5)
24 cm: (6,8,10)
30 cm: (5,12,13)
36 cm: (9,12,15)
40 cm: (8,15,17)
48 cm: (12,16,20)
In contrast, some lengths of wire, like 20 cm, cannot be bent to form an integer sided
right angle triangle, and other lengths allow more than one solution to be found; for
example, using 120 cm it is possible to form exactly three different integer sided
right angle triangles.
120 cm: (30,40,50), (20,48,52), (24,45,51)
Given that L is the length of the wire, for how many values of L ≤ 1,500,000 can
exactly one integer sided right angle triangle be formed?
"""
# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/28
'''
import pickle
with open("./commonfuncs/Primes6.dict", 'rb') as pfile:
pd = pickle.load(pfile)
def solution(limit: int = 1500000, answers: int = 1) -> int:
'''
L = a + b + c
c^2 = a^2 + b^2
so, L = a + b + sqrt(a^2 + b^2)
---> L must be even
a:b:c = m^2 - n^2 : 2mn : m^2 + n^2
assume,a = k(m^2 - n^2)
b = k(2mn)
c = k(m^2 + n^2)
4kn^2 < L = 2km(m + n) < 4km^2
so, if L/2 is prime, no sulution
'''
CandidateL = {l: [] for l in range(12, limit+1, 2) if not pd[l//2]}
for n in range(1, int(limit**0.5/2)+1):
for m in range(n+1, limit//2):
mm, nn, mn = m**2, n**2, m*n
k = limit // (2*(mm+mn))
if k:
for i in range(1, k+1):
c = i*(mm + nn)
l = 2*i*(mm+mn)
if c not in CandidateL[l]:
CandidateL[l].append(c)
# print(f'{l} = {i*(m*m - n*n)} + {2*i*m*n} +{c}')
else:
break
return len({key: val for key, val in CandidateL.items()
if len(val) == answers})
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=False)
print(solution())
# 161667
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kuperain/projecteuler.git
git@gitee.com:kuperain/projecteuler.git
kuperain
projecteuler
projecteuler
master

搜索帮助