Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created March 30, 2012 19:32
Show Gist options
  • Save kurtbrose/2254301 to your computer and use it in GitHub Desktop.
Save kurtbrose/2254301 to your computer and use it in GitHub Desktop.
table = {}
def f(n, k):
if (n, k) not in table: #compute if not yet visited
if n == 1:
table[n,k] = k-1
if n == 2:
table[n,k] = k*(k-1)
if n >= 3:
table[n,k] = (k-1)*f(n-1, k) + (k-1)*f(n-2, k)
return table[n,k]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment