Skip to content

Instantly share code, notes, and snippets.

@mainroach
Created March 21, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mainroach/6de3d6f61249c3396fa1cfe1c440cd1f to your computer and use it in GitHub Desktop.
Save mainroach/6de3d6f61249c3396fa1cfe1c440cd1f to your computer and use it in GitHub Desktop.
from fractions import Fraction
def pascDiagFast(row,length):
#compute the fractions of this diag
fracs=[1]*(length)
for i in range(length-1):
num = i+1
denom = row+1+i
fracs[i] = Fraction(num,denom)
#now let's compute the values
vals=[0]*length
#first figure out the leftmost tail of this diag
lowRow = row + (length-1)
lowRowCol = row
tail = pascalIndexInRowFast(lowRow,lowRowCol)
vals[-1] = tail
#walk backwards!
for i in reversed(range(length-1)):
vals[i] = int(fracs[i]*vals[i+1])
return vals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment