M
Mike__
Harlan
I'm way out of my comfort zone for knowing what i'm taking about but could I
ask just a couple more questions.
In my example I'm only interested in 5 numbers pulled out of the master list
ie in the example below only these combnations are relevant
And these aren't
If it not too much hassle could you post a macro for me that will do this
for me.
I've simplified my actual real life problem by saying its 5 from 20. But its
actually 11 from about 150. Will the macro able to handle this or is there
too much number crunching involved ?
Thanks very much for your assistance so far.
Mike
I'm way out of my comfort zone for knowing what i'm taking about but could I
ask just a couple more questions.
In my example I'm only interested in 5 numbers pulled out of the master list
ie in the example below only these combnations are relevant
9 8 7 5 1
10 9 8 2 1
10 9 7 2 2
10 9 5 5 1
10 8 5 5 2
12 10 5 2 1
12 9 5 2 2
12 8 7 2 1
12 7 5 5 1
And these aren't
12 10 8
10 8 7 5
12 10 7 1
12 9 8 1
12 9 7 2
12 8 5 5
9 8 5 5 2 1
9 7 5 5 2 2
10 8 7 2 2 1
10 7 5 5 2 1
12 8 5 2 2 1
If it not too much hassle could you post a macro for me that will do this
for me.
I've simplified my actual real life problem by saying its 5 from 20. But its
actually 11 from about 150. Will the macro able to handle this or is there
too much number crunching involved ?
Thanks very much for your assistance so far.
Mike
Harlan Grove said:Mike__ wrote...
........Say I have 10 numbers, and some will be duplicated.
eg 1,2,2,5,5,7,8,9,10,12
How can I produce a list of combinations of say 5 numbers that add up to say
30.
I want my list to include ALL combinations
ie 1 2 8 9 10 will appear twice as it will use a different number 2.
I think I need Dana's macro mentioned previously but please enlighten me.
Dana never provided the code he used to produce his results. I
speculated that he used Mathematica to generate all nonempty
combinations, then summed each of them. If so, that's a relatively
simple operation in Mathematica because it includes built-in means to
generate combinations and sum the combinations. It's not as easy in
Excel.
Worst case, this class of problem requires checking all 2^N
combinations. It's a practical necessity to eliminate unnecessary
branches and reduce unnecessary duplication from the iterative process.
That's why my macro doesn't produce multiple identical combinations
when there are duplicate numbers in the original set. Doing so requires
additional overhead that grows with the number of combinations in each
iterative step.
If you took the output from my macro, you have the distinct
combinations that sum to the target value. Use Data > Text to Columns
to split those into separate columns. If your original set were in
J5:J14 and the parsed (Data > Text to Columns) distinct combinations
were in L5:Q24, you could calculate the number of instances in K5:K24
using the following formulas.
K5 [array formula]:
=PRODUCT(IF(COUNTIF($J$5:$J$14,L5:Q5),
COUNTIF($J$5:$J$14,L5:Q5)/COUNTIF(L5:Q5,L5:Q5)))
Select K5 and fill down into K6:K24.
The distinct combinations of your original data that sum to 30 are
12 10 8
10 8 7 5
12 10 7 1
12 9 8 1
12 9 7 2
12 8 5 5
9 8 7 5 1
10 9 8 2 1
10 9 7 2 2
10 9 5 5 1
10 8 5 5 2
12 10 5 2 1
12 9 5 2 2
12 8 7 2 1
12 7 5 5 1
9 8 5 5 2 1
9 7 5 5 2 2
10 8 7 2 2 1
10 7 5 5 2 1
12 8 5 2 2 1
and the number of instances of each using the col K formulas above are
1
2
1
1
2
1
2
2
1
1
2
4
2
2
1
2
1
1
2
2
Macros are the only way to generate the necessary combinations with
some efficiency. Formulas are more efficient counting the instances of
each of the distinct combinations in the solution set.