I have a vague memory of having done this some time ago but I don’t remember the formula. What I need is all possible combinations of positive integers xi (i=1,..,k) that yield s when they are summed. For instance, for k=2 and s=2 there are the 3 combinations {0,2}, {1,1} and {2,0} the first number in a bracket representing x1 and the second x2. This can be done with the code
But I need a fast function as I will calculate it for very big k and s. So this code is too slow. Do you remember the formula involving factorials? Or is there a Mathematica function that does this?
Hey Bill, IntegerPartitions is almost what I am looking for but I need only the number of partitions not the partitions themselves. For the example I cited above the number is 3 as there are the partitions {0,2}, {1,1} and {2,0} for k=2 and s=2. For k=2 and s=3 it is 4 and for k=3 and s=2 it’s 6 and so on. Of course, I could count the elements with Length[IntegerPartitions[s,{k}]] but I think there is a direct formula if I remember well. Unfortunately I don’t find it… And apparently IntegerPartitions does not include the number 0.
Ah, I didn’t understand that. Is it perhaps PartitionsP that you were thinking of? And A000041 - OEIS for a lot of background information on the subject.
Well I assumed that 0 is a positive integer. Sorry if I am wrong… To be more precise, k>1 and s>1 but
xk>=0 for every k. By the way, it seems that the code above is not correct for some low values. I will be back tomorrow. Here in Europe it’s time to go sleep now…
But your function is better. By the way, in my code above for f[k,s] there is an error: the xi do not go from 0 to k but from 0 to s… I will edit it if I can.
Hey Mariusz, I still have a question. In fact I realized that what I need is the values of this function, which is almost the same as the one in my first post expect that the xi go from 0 to d (where d>1 is a positive integer) instead of going from 0 to s: