The money changing problem is a simple one to state. For example, how many different ways can one form change of a dollar (100 cents) by using only coins of denomination [1 5 10 25 50] ? (The answer is 292.)
Its an example of a general problem, i.e., in how many unique ways can an integer be partitioned as a sum of smaller positive integers?
I wrote partitions to solve the fully general problem, but it can be used with restrictions too. You can constrain the set of elements in the sum, and the maximum number of times any one elements can appear, as well as fixing the total number of terms that will appear in the final sum.
See the demo for a few examples of use.
Thanks a lot, I was looking for this function.
Suppose I wish to list out the integer partitions of 10, using the numbers [1,2,3,4]? The solutions are easy to delineate. Look at the largest element of the candidate members of the partition, in this case, pivot around the number 4. How many times can 4 potentially appear in that sum? Clearly, we may be able to form the total of 10 by including the number 4 a total of 0, 1, or 2 times. If so, then we can reduce the problem.
If 4 appears twice, then we need to find the list of all partitions of 10 - 2*4 = 2, made from the set [1,2,3].
If 4 appears once, then we need to find the list of all partitions of 10 - 1*4 = 6, made from the set [1,2,3].
If 4 appears not at all, then we need to find the list of all partitions of 10 - 0*4 = 10, made from the set [1,2,3].
I can rigorously argue that this procedure must generate all solutions, and do so rather efficiently, to the extent that is possible.
Now, call partitions to solve the problem. See that the last two solutions in the set have 4 appearing twice. There are exactly 2 ways to solve that problem. Before that, we see there were 7 ways to solve the partitions of 6. And there were 14 ways to solve the problem with 4 never appearing at all.
10 0 0 0
8 1 0 0
6 2 0 0
4 3 0 0
2 4 0 0
0 5 0 0
7 0 1 0
5 1 1 0
3 2 1 0
1 3 1 0
4 0 2 0
2 1 2 0
0 2 2 0
1 0 3 0
6 0 0 1
4 1 0 1
2 2 0 1
0 3 0 1
3 0 1 1
1 1 1 1
0 0 2 1
2 0 0 2
0 1 0 2
While partitions has other arguments that complicate the problem slightly by constraining the solution, the basic approach is exactly the same. Simply pivot around the largest member of the set of candidates to form that sum. Once you have determined the number of times 4 may appear in the sum, you can forget about it, and then worry about the rest.
Of course, had I tried to solve the problem partitions(500,1:500) by this approach, this would be a rather time (and memory) consuming task. My vpi toolbox has a tool to enumerate the number of integer solutions:
Or, if you prefer...
two sextillion, three hundred quintillion, one hundred sixty five quadrillion, thirty two trillion, five hundred seventy four billion, three hundred twenty three million, nine hundred ninety five thousand, twenty seven
But then, nothing I could do would solve that problem efficiently.
What algorithm is used or what is the theory behind how the partitions are generated by the program.
Thank you John! Its not an easy problem to solve yourself.
Excellent codes with helpful documentation.
Very good code! Very helpful!
a very useful snippet distinguished by excellent help (including sources of its algorithm), intuitive examples as well as a demo package, exhaustive error checking, good guidance for programmers, and a sleek computational engine.
Version 1.1: Allow the candidate set to be non-decreasing rather than strictly increasing