Skip to content

Add euler project problem 15 additional solution #12774

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
34 changes: 34 additions & 0 deletions project_euler/problem_015/sol2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
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?
"""

from numpy import integer, ones


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 = ones((n + 1, n + 1), dtype=integer)

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 int(counts[n][n])


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