Looks like you are describing the classic problem of counting the number of ways to make change, given coins of value 1, 2, 5, and 10.
Generating functions can count the ways.
GFCoinPartitions[n_, d_List] :=
Block[{z},
Coefficient[Series[1/Times @@ (1 - z^d), {z, 0, n}], z, n]
]
For your case, the input list of denominations is d={1,2,5,10}
, and n
is the sum you want. For example,
Table[GFCoinPartitions[i,{1,2,5,10}],{i,1,10}]
{1, 2, 2, 3, 4, 5, 6, 7, 8, 11}
If you really want some speed, then consider SeriesCoefficient
.
messyexpression =
SeriesCoefficient[
1/((1 - z) (1 - z^2) (1 - z^5) (1 - z^10)),
{z, 0, n}, Assumptions :> n > 0]
Use this as follows to get counts up to 120.
Round[messyexpression /. n -> Range[1, 120]]
Much, much faster is the rather arcane code below. This gives the first 120 counts in less than a millisecond.
Block[{n = 120, c, t},
c = ConstantArray[1, n + 1];
t = c;
Do[
Do[t[[Range[i + 1, n + 1]]] += c[[Range[n + 1 - i]]], {i, k, n, k}];
c = t,
{k, {2, 5, 10}}];
c
]