Skip to content

Commit 687726d

Browse files
Update sol1.py
1 parent 739a54e commit 687726d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

project_euler/problem_164/sol1.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def solve(
1414
digit: int, prev1: int, prev2: int, sum_max: int, first: bool, cache: dict[str, int]
1515
) -> int:
1616
"""
17-
Solve for remaining 'digit' digits, with previous 'prev1' number, and
18-
previous-previous 'prev2' number, total sum of 'sum_max'.
17+
Solve for remaining 'digit' digits, with previous 'prev1' digit, and
18+
previous-previous 'prev2' digit, total sum of 'sum_max'.
1919
Pass around 'cache' to store/reuse intermediate results.
2020
2121
>>> solve(digit=1, prev1=0, prev2=0, sum_max=9, first=True, cache={})
@@ -25,21 +25,25 @@ def solve(
2525
"""
2626
if digit == 0:
2727
return 1
28-
comb = 0
28+
2929
cache_str = f"{digit},{prev1},{prev2}"
3030
if cache_str in cache:
3131
return cache[cache_str]
32-
for v in range(sum_max - prev1 - prev2 + 1):
33-
if first and v == 0:
32+
33+
comb = 0
34+
for curr in range(sum_max - prev1 - prev2 + 1):
35+
if first and curr == 0:
3436
continue
37+
3538
comb += solve(
3639
digit=digit - 1,
37-
prev1=v,
40+
prev1=curr,
3841
prev2=prev1,
3942
sum_max=sum_max,
4043
first=False,
4144
cache=cache,
4245
)
46+
4347
cache[cache_str] = comb
4448
return comb
4549

0 commit comments

Comments
 (0)