Skip to content

Instantly share code, notes, and snippets.

@jmikkola
Created February 17, 2018 04:33
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 jmikkola/36de332688cd875cacf5a235b8f9fba8 to your computer and use it in GitHub Desktop.
Save jmikkola/36de332688cd875cacf5a235b8f9fba8 to your computer and use it in GitHub Desktop.
def f(k, n):
'''
k indistinct bucket,
n (n > k) balls,
each bucket must have at least one ball,
how many ways are there to put n balls in k buckets?
'''
if k == 1:
# Only one way to put things in one bucket
return 1
elif n == k:
# Each bucket has exactly one ball
return 1
else:
# If the nth ball is by itself, it and its bucket can be ignored, giving
# the f(k - 1, n - 1) term.
# On the other hand, if the nth ball is in some bucket that is already
# occupied, that is equivalent to solving the problem without the nth
# ball, hence f(k, n - 1), multiplied by the ways the nth ball could be
# added to that.
return f(k - 1, n - 1) + k * f(k, n - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment