Skip to content

Instantly share code, notes, and snippets.

@tyler
Created May 24, 2010 20:08
Show Gist options
  • Save tyler/412367 to your computer and use it in GitHub Desktop.
Save tyler/412367 to your computer and use it in GitHub Desktop.
# 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