Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Project Euler problem 94 solution 1 #8599

3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
* [Longest Sub Array](dynamic_programming/longest_sub_array.py)
* [Matrix Chain Order](dynamic_programming/matrix_chain_order.py)
* [Max Non Adjacent Sum](dynamic_programming/max_non_adjacent_sum.py)
* [Max Product Subarray](dynamic_programming/max_product_subarray.py)
* [Max Sub Array](dynamic_programming/max_sub_array.py)
* [Max Sum Contiguous Subsequence](dynamic_programming/max_sum_contiguous_subsequence.py)
* [Min Distance Up Bottom](dynamic_programming/min_distance_up_bottom.py)
Expand Down Expand Up @@ -936,6 +937,8 @@
* [Sol1](project_euler/problem_091/sol1.py)
* Problem 092
* [Sol1](project_euler/problem_092/sol1.py)
* Problem 094
* [Sol1](project_euler/problem_094/sol1.py)
* Problem 097
* [Sol1](project_euler/problem_097/sol1.py)
* Problem 099
Expand Down
Empty file.
44 changes: 44 additions & 0 deletions project_euler/problem_094/sol1.py
Original file line number Diff line number Diff line change
@@ -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() = }")