Created
February 17, 2018 04:33
-
-
Save jmikkola/36de332688cd875cacf5a235b8f9fba8 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
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