File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
project_euler/problem_015 Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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 ())
You can’t perform that action at this time.
0 commit comments