Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@
* [Sol2](project_euler/problem_014/sol2.py)
* Problem 015
* [Sol1](project_euler/problem_015/sol1.py)
* [Sol2](project_euler/problem_015/sol2.py)
* Problem 016
* [Sol1](project_euler/problem_016/sol1.py)
* [Sol2](project_euler/problem_016/sol2.py)
Expand Down
31 changes: 31 additions & 0 deletions project_euler/problem_015/sol2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Problem 15: https://projecteuler.net/problem=15

Starting in the top left corner of a 2x2 grid, and only being able to move to
the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20x20 grid?
"""

def solution(n: int = 20) -> int:
"""
Solve by explicitly counting the paths with dynamic programming.

>>> solution(6)
924
>>> solution(2)
6
>>> solution(1)
2
"""

counts = [[1 for _ in range(n + 1)] for _ in range(n + 1)]

for i in range(1, n + 1):
for j in range(1, n + 1):
counts[i][j] = counts[i - 1][j] + counts[i][j - 1]

return counts[n][n]


if __name__ == "__main__":
print(solution())
Loading