Created
May 24, 2010 20:08
-
-
Save tyler/412367 to your computer and use it in GitHub Desktop.
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
# Returns an array representing a specific level of Pascal's triangle | |
def pascal1(n) | |
if n == 1 | |
[1] | |
else | |
nl = [0] + pascal1(n-1) + [0] | |
l = [] | |
(nl.size - 1).times do |i| | |
l << nl[i] + nl[i+1] | |
end | |
l | |
end | |
end | |
# Returns the number at a specified level and index of Pascal's triangle | |
def pascal2(level,element) | |
if element < 0 || element > level | |
0 | |
elsif level == 0 | |
1 | |
else | |
pascal2(level - 1, element - 1) + pascal2(level - 1, element) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment