Created
May 29, 2011 07:48
-
-
Save johana-star/997548 to your computer and use it in GitHub Desktop.
Euler Project Problem #015
This file contains hidden or 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
# Solution to Project Euler's fifteenth problem | |
# http://projecteuler.net/index.php?section=problems&id=15 | |
# How many routes are there through a 2020 grid? | |
def generate_Pascals_triangle(depth) | |
triangle = [[1], [1,1]] | |
(2..depth).each do |d| | |
# generate row # | |
triangle[d] = [] | |
next_row = triangle[d-1] | |
next_row.push 0; next_row.unshift 0 | |
(1..(next_row.length-1)).each do |o| | |
triangle[d].push (next_row[o-1] + next_row[o]) | |
end | |
end | |
return triangle | |
end | |
number = 40 | |
triangle = generate_Pascals_triangle(number) | |
p triangle[40][20] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment