Python Implementation of Problem 15

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 15

Turns out this is easy, if you think sideways a bit

You can only go down or right. If we say right=1, then you can only have 20 1s, since otherwise you go off the grid. You also can't have fewer than 20 1s, since then you go off the grid the other way. This means you can look at it as a bit string, and the number of 40-bit strings with 20 1s is 40c20.

Problem:

Starting in the top left corner of a 2×2 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 20×20 grid?

python.src.p0015.main() int
 1"""
 2Project Euler Problem 15
 3
 4Turns out this is easy, if you think sideways a bit
 5
 6You can only go down or right. If we say right=1, then you can only have 20 1s, since otherwise you go off the grid.
 7You also can't have fewer than 20 1s, since then you go off the grid the other way. This means you can look at it as a
 8bit string, and the number of 40-bit strings with 20 1s is 40c20.
 9
10Problem:
11
12Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6
13routes to the bottom right corner.
14
15How many such routes are there through a 20×20 grid?
16
17"""
18from .lib.math import lattice_paths
19
20
21def main() -> int:
22    return lattice_paths(20, 20)

Tags: combinatorics