Skip to content

Commit 383ec91

Browse files
mindauglMaximSmolskiypre-commit-ci[bot]cclauss
authored
Add euler project problem 15 additional solution (#12774)
* Add euler project problem 15 additional solution by explicitly counting the paths. * Update sol2.py * updating DIRECTORY.md * updating DIRECTORY.md * Trigger CI * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: MaximSmolskiy <MaximSmolskiy@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: cclauss <3709715+cclauss@users.noreply.github.com>
1 parent 76deea4 commit 383ec91

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@
986986
* [Sol2](project_euler/problem_014/sol2.py)
987987
* Problem 015
988988
* [Sol1](project_euler/problem_015/sol1.py)
989+
* [Sol2](project_euler/problem_015/sol2.py)
989990
* Problem 016
990991
* [Sol1](project_euler/problem_016/sol1.py)
991992
* [Sol2](project_euler/problem_016/sol2.py)

project_euler/problem_015/sol2.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
10+
def solution(n: int = 20) -> int:
11+
"""
12+
Solve by explicitly counting the paths with dynamic programming.
13+
14+
>>> solution(6)
15+
924
16+
>>> solution(2)
17+
6
18+
>>> solution(1)
19+
2
20+
"""
21+
22+
counts = [[1 for _ in range(n + 1)] for _ in range(n + 1)]
23+
24+
for i in range(1, n + 1):
25+
for j in range(1, n + 1):
26+
counts[i][j] = counts[i - 1][j] + counts[i][j - 1]
27+
28+
return counts[n][n]
29+
30+
31+
if __name__ == "__main__":
32+
print(solution())

0 commit comments

Comments
 (0)