Skip to content

Commit 2e1892e

Browse files
committed
Add euler project problem 15 additional solution by explicitly counting the paths.
1 parent a2fa32c commit 2e1892e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

project_euler/problem_015/sol2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Problem 15: https://projecteuler.net/problem=15
3+
4+
Starting in the top left corner of a 2x2 grid, and only being able to move to
5+
the right and down, there are exactly 6 routes to the bottom right corner.
6+
How many such routes are there through a 20x20 grid?
7+
"""
8+
9+
from numpy import integer, ones
10+
11+
12+
def solution(n: int = 20) -> int:
13+
"""
14+
Solve by explicitly counting the paths with dynamic programming.
15+
16+
>>> solution(6)
17+
924
18+
>>> solution(2)
19+
6
20+
>>> solution(1)
21+
2
22+
"""
23+
24+
counts = ones((n + 1, n + 1), dtype=integer)
25+
26+
for i in range(1, n + 1):
27+
for j in range(1, n + 1):
28+
counts[i][j] = counts[i - 1][j] + counts[i][j - 1]
29+
30+
return int(counts[n][n])
31+
32+
33+
if __name__ == "__main__":
34+
print(solution())

0 commit comments

Comments
 (0)