代码拉取完成,页面将自动刷新
"""
Problem 94: https://projecteuler.net/problem=94
It is easily proved that no equilateral triangle exists with
integral length sides and integral area. However, the almost
equilateral triangle 5-5-6 has an area of 12 square units.
We shall define an almost equilateral triangle to be a triangle
for which two sides are equal and the third differs by no more
than one unit.
Find the sum of the perimeters of all almost equilateral triangles
with integral side lengths and area and whose perimeters do not
exceed one billion (1,000,000,000).
"""
def triangleArea(a, b, c):
'''
>>> assert triangleArea(5,5,6) == 12
>>> print(triangleArea(302828,302828,302829))
39709429597.0
'''
p = (a+b+c)/2
return (p*(p-a)*(p-b)*(p-c))**0.5
def almostEquilateralTriangleArea(e):
'''
>>> print(almostEquilateralTriangleArea(302828))
39709429597.0
'''
ee = e**2
return ((3*ee+4*e+1)*(ee-1)/16)**0.5
def solution(perimeterLimit: int = 1000000000) -> int:
res = 0
equalSide = 2
limit = (perimeterLimit-1)//3
while equalSide <= limit:
area = almostEquilateralTriangleArea(equalSide)
if area == int(area):
print(equalSide, area)
res += equalSide*3+1
equalSide+=1
return res
if __name__ == "__main__":
from doctest import testmod
testmod()
print(solution())
# 156274530155969160
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。