Skip to content

Commit ecb986b

Browse files
MaximSmolskiygithub-actionspre-commit-ci[bot]
authored andcommitted
Add Project Euler problem 94 solution 1 (TheAlgorithms#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>
1 parent f1a50d1 commit ecb986b

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@
937937
* [Sol1](project_euler/problem_091/sol1.py)
938938
* Problem 092
939939
* [Sol1](project_euler/problem_092/sol1.py)
940+
* Problem 094
941+
* [Sol1](project_euler/problem_094/sol1.py)
940942
* Problem 097
941943
* [Sol1](project_euler/problem_097/sol1.py)
942944
* Problem 099

Diff for: project_euler/problem_094/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_094/sol1.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Project Euler Problem 94: https://projecteuler.net/problem=94
3+
4+
It is easily proved that no equilateral triangle exists with integral length sides and
5+
integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square
6+
units.
7+
8+
We shall define an almost equilateral triangle to be a triangle for which two sides are
9+
equal and the third differs by no more than one unit.
10+
11+
Find the sum of the perimeters of all almost equilateral triangles with integral side
12+
lengths and area and whose perimeters do not exceed one billion (1,000,000,000).
13+
"""
14+
15+
16+
def solution(max_perimeter: int = 10**9) -> int:
17+
"""
18+
Returns the sum of the perimeters of all almost equilateral triangles with integral
19+
side lengths and area and whose perimeters do not exceed max_perimeter
20+
21+
>>> solution(20)
22+
16
23+
"""
24+
25+
prev_value = 1
26+
value = 2
27+
28+
perimeters_sum = 0
29+
i = 0
30+
perimeter = 0
31+
while perimeter <= max_perimeter:
32+
perimeters_sum += perimeter
33+
34+
prev_value += 2 * value
35+
value += prev_value
36+
37+
perimeter = 2 * value + 2 if i % 2 == 0 else 2 * value - 2
38+
i += 1
39+
40+
return perimeters_sum
41+
42+
43+
if __name__ == "__main__":
44+
print(f"{solution() = }")

0 commit comments

Comments
 (0)