I'm calculating how many possibilities there are to fit \$n\$ identical bottles inside \$k\$ crates of various capacities.
- n = Number of bottles
- k = Number of crates
- K = List of the number of bottles each crate can fit
Each crate may be filled with any number of bottles up to \$K_i\$, including being empty.
This is an instance of the Donut Shop Problem.
Example 1
Let's say we have:
- 7 Bottles (\$n = 7\$)
- 2 Crates (\$k = 2\$)
- Crate 1 fits 3 bottles, crate 2 fits 5 bottles (\$K_1 = 3, K_2 = 5\$)
There are 2 possibilities to fit the bottles inside the crates.
Example 2
- 7 Bottles (\$n = 7\$)
- 23 Crates (\$k = 2\$\$k = 3\$)
- Crate 1 fits 2 bottles, crate 2 fits 3 bottles, crate 3 fits 4 bottles (\$K_1 = 2, K_2 = 3, K_3 = 4\$)
6 possibilities
I have a recursive solution that works fine for small integers:
def T(n, k, K):
if k==0: return n==0
return sum(T(n-i, k-1, K) for i in xrange(0, K[k-1]+1))
But when I try it with \$n = 30, k= 20, K_i = i\$, it takes FOREVER, so I'm asking you: how could I improve the above code/function?