1 Star 0 Fork 0

阿坚/projecteuler

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

搜索帮助