代码拉取完成,页面将自动刷新
"""
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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。