mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Add Project Euler problem 94 solution 1 (#8599)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
d66e1e8732
commit
3d2012c4ba
|
@ -937,6 +937,8 @@
|
||||||
* [Sol1](project_euler/problem_091/sol1.py)
|
* [Sol1](project_euler/problem_091/sol1.py)
|
||||||
* Problem 092
|
* Problem 092
|
||||||
* [Sol1](project_euler/problem_092/sol1.py)
|
* [Sol1](project_euler/problem_092/sol1.py)
|
||||||
|
* Problem 094
|
||||||
|
* [Sol1](project_euler/problem_094/sol1.py)
|
||||||
* Problem 097
|
* Problem 097
|
||||||
* [Sol1](project_euler/problem_097/sol1.py)
|
* [Sol1](project_euler/problem_097/sol1.py)
|
||||||
* Problem 099
|
* Problem 099
|
||||||
|
|
0
project_euler/problem_094/__init__.py
Normal file
0
project_euler/problem_094/__init__.py
Normal file
44
project_euler/problem_094/sol1.py
Normal file
44
project_euler/problem_094/sol1.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
"""
|
||||||
|
Project Euler 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 solution(max_perimeter: int = 10**9) -> int:
|
||||||
|
"""
|
||||||
|
Returns the sum of the perimeters of all almost equilateral triangles with integral
|
||||||
|
side lengths and area and whose perimeters do not exceed max_perimeter
|
||||||
|
|
||||||
|
>>> solution(20)
|
||||||
|
16
|
||||||
|
"""
|
||||||
|
|
||||||
|
prev_value = 1
|
||||||
|
value = 2
|
||||||
|
|
||||||
|
perimeters_sum = 0
|
||||||
|
i = 0
|
||||||
|
perimeter = 0
|
||||||
|
while perimeter <= max_perimeter:
|
||||||
|
perimeters_sum += perimeter
|
||||||
|
|
||||||
|
prev_value += 2 * value
|
||||||
|
value += prev_value
|
||||||
|
|
||||||
|
perimeter = 2 * value + 2 if i % 2 == 0 else 2 * value - 2
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return perimeters_sum
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(f"{solution() = }")
|
Loading…
Reference in New Issue
Block a user