Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f98f17
commit 791c2ea
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
Problem 15 | ||
Starting in the top left corner of a 2x2 grid, there are 6 routes (without backtracking) to the bottom right corner. | ||
How many routes are there through a 20x20 grid? | ||
""" | ||
def pascals_triangle(n): | ||
x=[[1]] | ||
for i in range(n-1): | ||
x.append([sum(i) for i in zip([0]+x[-1],x[-1]+[0])]) | ||
return x | ||
|
||
def solve(): | ||
# It turns out that the number of paths follow the pattern as | ||
# The middle number of pascal triangle in the odd numbered rows | ||
# For a 20x20 grid | ||
rows = 20 | ||
columns = 20 | ||
triangle = pascals_triangle(2*rows + 1) | ||
return triangle[2*rows][columns] | ||
|
||
print "Answer by traditional method:", solve() |