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>
This commit is contained in:
Mindaugas
2026-03-12 23:00:28 +02:00
committed by GitHub
parent c34f23e47e
commit a2efba59bf
2 changed files with 38 additions and 0 deletions

View File

@@ -469,6 +469,11 @@
## Geometry ## Geometry
* [Geometry](geometry/geometry.py) * [Geometry](geometry/geometry.py)
* [Graham Scan](geometry/graham_scan.py)
* [Jarvis March](geometry/jarvis_march.py)
* Tests
* [Test Graham Scan](geometry/tests/test_graham_scan.py)
* [Test Jarvis March](geometry/tests/test_jarvis_march.py)
## Graphics ## Graphics
* [Bezier Curve](graphics/bezier_curve.py) * [Bezier Curve](graphics/bezier_curve.py)
@@ -981,6 +986,7 @@
* [Sol2](project_euler/problem_014/sol2.py) * [Sol2](project_euler/problem_014/sol2.py)
* Problem 015 * Problem 015
* [Sol1](project_euler/problem_015/sol1.py) * [Sol1](project_euler/problem_015/sol1.py)
* [Sol2](project_euler/problem_015/sol2.py)
* Problem 016 * Problem 016
* [Sol1](project_euler/problem_016/sol1.py) * [Sol1](project_euler/problem_016/sol1.py)
* [Sol2](project_euler/problem_016/sol2.py) * [Sol2](project_euler/problem_016/sol2.py)

View File

@@ -0,0 +1,32 @@
"""
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())