View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.worksheet.functions
Martin Brown Martin Brown is offline
external usenet poster
 
Posts: 230
Default XL 2003: How do I get every possible combination?

KC wrote:
I must have misread the requirements.
If I have 4 numbers to sum,
from one single number - I have 4 numbers
from two numbers - I have 4 x 3 numbers
from three numbers - I have 4 x 3 x 2 numbers


But that still includes the same numbers in different orders and since
addition is commutative you can reduce the amount of work needed. It
doesn't help much but it does reduce things slightly from

O( N^(N+1/2) )

to a mere O(2^N )

Either way the computational difficulty of the problem increases
exponentially with the number of numbers you want to combine.

Given N distinct numbers taken M at a time the number of possible sums
that can be made is

s = N!/(M!(N-M!)

For your concrete example of 4 numbers this gives

N 0 1 2 3 4
s 1 4 6 4 1

Which as another poster pointed out yields 2^N-1 values.

why do I have only 2 loops for 162 numbers?


Because you don't have a clue what you are doing.

You would need 161 nested loops or a recursive implementation to do the
problem as stated and it would take a time much greater than the age of
the universe even on the worlds fastest machine.

It is a tractable problem only for relatively modest values of N,M.

If it is a homework question then you were probably meant to compute the
sum of every pair or something like that. N(N-1)/2

Regards,
Martin Brown

first number + 2nd number = 2nd number + first number
So I must also get rid of sums of equal values


Hello all,

I'm looking for an XL solution and/or a VBA solution...and I just can't
wrap my head around this.

I have a list of 162 numbers (loan amounts: min = $0, max = $3,107,000).

I want to find the sum of every single combination of the 162
numbers...from one single number up to all 162 numbers. If my
calculations are correct, that is 26,244 different sums.

For VBA, I'm guessing I'm gonna need 2 for loops...one nested inside the
other. But I can't figure out the code to sum up all the different
combinations. Something like this:


For plngIndexX = LBound(pdblArray) To UBound(pdblArray)
For plngIndexY = LBound(pdblArray) To UBound(pdblArray)
'code to calculate all the differnt combinations
Next plngIndexY
Next plngIndexX


In XL, trying to think like a computer...I'm gonna need 162 bits(cells) to
calculate. But instead of each bit having a progressive value of a power
of 2, each bit will be one of the numbers in my list. I was thinking of
using the list of numbers as column headers across the top, have a
sumproduct formula in column 163/164 that will sum up the column headers
according to the bits being on or off, and then have 26,000+ rows of bits
marked on or off. The problem would be filling out those 26,000 rows with
1's & 0's. Anyone know of formulas or code that I could use to mark my
bits on or off?

If it would be easier/faster in MS Access/SQL Server, I'm all ears.

Thanks for any help anyone can provide,

Conan Kelly