Skip to content

Commit f86430b

Browse files
committed
Add initial version for euler project problem 122.
1 parent 0c8cf8e commit f86430b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Diff for: project_euler/problem_122/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_122/sol1.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Project Euler Problem 122: https://projecteuler.net/problem=122
3+
4+
Efficient Exponentiation
5+
"""
6+
7+
8+
def solve(nums: list[int], goal: int, depth: int) -> bool:
9+
if len(nums) > depth:
10+
return False
11+
for el in nums:
12+
if el + nums[-1] == goal:
13+
return True
14+
nums.append(el + nums[-1])
15+
if solve(nums, goal, depth):
16+
return True
17+
del nums[-1]
18+
return False
19+
20+
21+
def solution(n: int = 200) -> int:
22+
m = [0]
23+
for i in range(2, n + 1):
24+
max_length = 0
25+
while True:
26+
nums = [1]
27+
max_length += 1
28+
if solve(nums, i, max_length):
29+
break
30+
m.append(len(nums))
31+
return sum(m)
32+
33+
34+
if __name__ == "__main__":
35+
print(f"{solution() = }")

0 commit comments

Comments
 (0)