Home All Groups Group Topic Archive Search About
Author
7 Mar 2009 4:08 PM
Ron Weiner
I have an array of int's.  I need to find an algorithm that computes the sum
of every combination of numbers in the array.  So for instance if I had an
array of 6 ints (lets say 5, 7, 10, 18, 24, and 33). I need to compute the
sum of all possible combinations of these numbers.

5
5+7
5+7+10
5+7+10+18
5+7+10+18+24
5+7+10+18+24+33
5+10
5+10+18
5+10+18+24
5+10+18+24+33

ad nauseum till I have computed the sum of every possible combination of the
six numbers.  The plan is to stuff all of the possible results into a
database table for additional processing later.

Somewhere in the back of my six plus decade old brain there is a high school
math teacher (probably long dead by now) saying "I told you that you would
need this one day".  Shoulda' paid more attention!  Can any of you kind
folks help me out.  Many thanks in advance.

R Weiner
www.WorksRite.com

Author
7 Mar 2009 6:07 PM
Larry Serflaten
"Ron Weiner" <rweineratworksritedotcom> wrote
> I have an array of int's.  I need to find an algorithm that computes the sum
> of every combination of numbers in the array.  So for instance if I had an
> array of 6 ints (lets say 5, 7, 10, 18, 24, and 33). I need to compute the
> sum of all possible combinations of these numbers.

> R Weiner
> www.WorksRite.com

If your array has few than 30 elements, you can simply count
your way through.  See if this will get you going....

LFS


Private Sub Form_Load()
  Debug.Print "Assuming an array with indexes 1 to 4"
  Debug.Print "Sum the values at these indexes:"
  Perm 4
  Debug.Print "Assuming an array with indexes 1 to 6"
  Debug.Print "Sum the values at these indexes:"
  Perm 6
End Sub

Sub Perm(ByVal Max As Long)
Dim cnt As Long, idx As Long, bit As Long

  For cnt = 1 To 2 ^ Max - 1
    idx = 1
    bit = cnt
    While bit > 0
      If bit And 1 Then
        ' add to result: tot = tot + data(idx)
        Debug.Print idx; " ";
      End If
      bit = bit \ 2
      idx = idx + 1
    Wend
    Debug.Print
  Next
End Sub
Author
7 Mar 2009 6:51 PM
Ron Weiner
Larry

You are the man!  Thanks for paying attention in High School.  Works Great!

Ron W
Show quoteHide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:OQJn590nJHA.5728@TK2MSFTNGP06.phx.gbl...
>
> "Ron Weiner" <rweineratworksritedotcom> wrote
>> I have an array of int's.  I need to find an algorithm that computes the
>> sum
>> of every combination of numbers in the array.  So for instance if I had
>> an
>> array of 6 ints (lets say 5, 7, 10, 18, 24, and 33). I need to compute
>> the
>> sum of all possible combinations of these numbers.
>
>> R Weiner
>> www.WorksRite.com
>
> If your array has few than 30 elements, you can simply count
> your way through.  See if this will get you going....
>
> LFS
>
>
> Private Sub Form_Load()
>  Debug.Print "Assuming an array with indexes 1 to 4"
>  Debug.Print "Sum the values at these indexes:"
>  Perm 4
>  Debug.Print "Assuming an array with indexes 1 to 6"
>  Debug.Print "Sum the values at these indexes:"
>  Perm 6
> End Sub
>
> Sub Perm(ByVal Max As Long)
> Dim cnt As Long, idx As Long, bit As Long
>
>  For cnt = 1 To 2 ^ Max - 1
>    idx = 1
>    bit = cnt
>    While bit > 0
>      If bit And 1 Then
>        ' add to result: tot = tot + data(idx)
>        Debug.Print idx; " ";
>      End If
>      bit = bit \ 2
>      idx = idx + 1
>    Wend
>    Debug.Print
>  Next
> End Sub
>
>
Author
7 Mar 2009 6:52 PM
Richard Mueller [MVP]
Show quote Hide quote
"Larry Serflaten" <serfla***@usinternet.com> wrote in message
news:OQJn590nJHA.5728@TK2MSFTNGP06.phx.gbl...
>
> "Ron Weiner" <rweineratworksritedotcom> wrote
>> I have an array of int's.  I need to find an algorithm that computes the
>> sum
>> of every combination of numbers in the array.  So for instance if I had
>> an
>> array of 6 ints (lets say 5, 7, 10, 18, 24, and 33). I need to compute
>> the
>> sum of all possible combinations of these numbers.
>
>> R Weiner
>> www.WorksRite.com
>
> If your array has few than 30 elements, you can simply count
> your way through.  See if this will get you going....
>
> LFS
>
>
> Private Sub Form_Load()
>  Debug.Print "Assuming an array with indexes 1 to 4"
>  Debug.Print "Sum the values at these indexes:"
>  Perm 4
>  Debug.Print "Assuming an array with indexes 1 to 6"
>  Debug.Print "Sum the values at these indexes:"
>  Perm 6
> End Sub
>
> Sub Perm(ByVal Max As Long)
> Dim cnt As Long, idx As Long, bit As Long
>
>  For cnt = 1 To 2 ^ Max - 1
>    idx = 1
>    bit = cnt
>    While bit > 0
>      If bit And 1 Then
>        ' add to result: tot = tot + data(idx)
>        Debug.Print idx; " ";
>      End If
>      bit = bit \ 2
>      idx = idx + 1
>    Wend
>    Debug.Print
>  Next
> End Sub
>

Clever. Arrays are almost always zero based, so I would use:

tot = tot + data(idx - 1)

Also, the value of tot should be initialized to zero. For example (assuming
data is defined elsewhere with module level scope):

Sub Perm(ByVal Max As Long)
  Dim cnt As Long, idx As Long, bit As Long, tot As Long

  For cnt = 1 To 2 ^ Max - 1
    idx = 1
    bit = cnt
    tot = 0
    While bit > 0
      If bit And 1 Then
        tot = tot + data(idx - 1)
      End If
      bit = bit \ 2
      idx = idx + 1
    Wend
    Debug.Print tot
  Next
End Sub

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Author
7 Mar 2009 7:31 PM
Larry Serflaten
"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote

> Clever. Arrays are almost always zero based, so I would use:

Thus the need for the printed assumption....

>
> tot = tot + data(idx - 1)
>
> Also, the value of tot should be initialized to zero. For example (assuming
> data is defined elsewhere with module level scope):

I thought about adding that initialization to the comments, but then again,
I didn't use, or declare tot.  It was only an example to solidify the fact that
the values printed were indices, and not the actual operands...

LFS
Author
7 Mar 2009 6:43 PM
DanS
Show quote Hide quote
"Ron Weiner" <rweineratworksritedotcom> wrote in
news:##Re07znJHA.1168@TK2MSFTNGP05.phx.gbl:

> I have an array of int's.  I need to find an algorithm that computes
> the sum of every combination of numbers in the array.  So for instance
> if I had an array of 6 ints (lets say 5, 7, 10, 18, 24, and 33). I
> need to compute the sum of all possible combinations of these numbers.
>
> 5
> 5+7
> 5+7+10
> 5+7+10+18
> 5+7+10+18+24
> 5+7+10+18+24+33
> 5+10
> 5+10+18
> 5+10+18+24
> 5+10+18+24+33
>
> ad nauseum till I have computed the sum of every possible combination
> of the six numbers.  The plan is to stuff all of the possible results
> into a database table for additional processing later.
>
> Somewhere in the back of my six plus decade old brain there is a high
> school math teacher (probably long dead by now) saying "I told you
> that you would need this one day".  Shoulda' paid more attention!  Can
> any of you kind folks help me out.  Many thanks in advance.
>
> R Weiner
> www.WorksRite.com

Interesting....there's probably a better way....but here's one.

Six numbers.....think of as six bits. In counting for 1 to 63 (highest
number 6 bits reperesents), each possible bit combination is represented.

You can assign the number to an array of 1 to 6, or 0 to 5, or whatever.

Then.....

Dim x(5) As Long
Dim y As Long
Dim z As Long

x(0) = 12
x(1) = 56
x(2) = 21
x(3) = 43
x(4) = 51
x(5) = 7

Dim total As Long
For y = 1 To 63
   For z = 0 To 5
          'Compare each bit in the y number
       If (y And (2 ^ z)) = (2 ^ z) Then
           Debug.Print x(z) & " + ";
           total = total + x(z)
       End If
   Next
   Debug.Print "= " & total
   total = 0
Next

......or similar....thee's something wrong here though, as we were taught
that the number of group subsets was 2^(count of numbers). 2^6 = 64, so
maybe there's something wrong above, but I'm sure you get the gist of it.
Author
7 Mar 2009 6:58 PM
Ron Weiner
Thanks Dan

Your code is very similar to Larry's and produces identical results.

Ron W
Show quoteHide quote
"DanS" <t.h.i.s.n.t.h.a.t@r.o.a.d.r.u.n.n.e.r.c.o.m> wrote in message
news:Xns9BC78BD0C75FDthisnthatroadrunnern@85.214.105.209...
> "Ron Weiner" <rweineratworksritedotcom> wrote in
> news:##Re07znJHA.1168@TK2MSFTNGP05.phx.gbl:
>
>> I have an array of int's.  I need to find an algorithm that computes
>> the sum of every combination of numbers in the array.  So for instance
>> if I had an array of 6 ints (lets say 5, 7, 10, 18, 24, and 33). I
>> need to compute the sum of all possible combinations of these numbers.
>>
>> 5
>> 5+7
>> 5+7+10
>> 5+7+10+18
>> 5+7+10+18+24
>> 5+7+10+18+24+33
>> 5+10
>> 5+10+18
>> 5+10+18+24
>> 5+10+18+24+33
>>
>> ad nauseum till I have computed the sum of every possible combination
>> of the six numbers.  The plan is to stuff all of the possible results
>> into a database table for additional processing later.
>>
>> Somewhere in the back of my six plus decade old brain there is a high
>> school math teacher (probably long dead by now) saying "I told you
>> that you would need this one day".  Shoulda' paid more attention!  Can
>> any of you kind folks help me out.  Many thanks in advance.
>>
>> R Weiner
>> www.WorksRite.com
>
> Interesting....there's probably a better way....but here's one.
>
> Six numbers.....think of as six bits. In counting for 1 to 63 (highest
> number 6 bits reperesents), each possible bit combination is represented.
>
> You can assign the number to an array of 1 to 6, or 0 to 5, or whatever.
>
> Then.....
>
> Dim x(5) As Long
> Dim y As Long
> Dim z As Long
>
> x(0) = 12
> x(1) = 56
> x(2) = 21
> x(3) = 43
> x(4) = 51
> x(5) = 7
>
> Dim total As Long
> For y = 1 To 63
>   For z = 0 To 5
>      'Compare each bit in the y number
>       If (y And (2 ^ z)) = (2 ^ z) Then
>           Debug.Print x(z) & " + ";
>           total = total + x(z)
>       End If
>   Next
>   Debug.Print "= " & total
>   total = 0
> Next
>
> .....or similar....thee's something wrong here though, as we were taught
> that the number of group subsets was 2^(count of numbers). 2^6 = 64, so
> maybe there's something wrong above, but I'm sure you get the gist of it.
Author
7 Mar 2009 10:24 PM
DanS
"Ron Weiner" <rweineratworksritedotcom> wrote in
news:O02LAb1nJHA.5124@TK2MSFTNGP03.phx.gbl:

> Thanks Dan
>
> Your code is very similar to Larry's and produces identical results.

Good to know, thanks.


Regards,

DanS



Show quoteHide quote
>
> Ron W
> "DanS" <t.h.i.s.n.t.h.a.t@r.o.a.d.r.u.n.n.e.r.c.o.m> wrote in message
> news:Xns9BC78BD0C75FDthisnthatroadrunnern@85.214.105.209...
>> "Ron Weiner" <rweineratworksritedotcom> wrote in
>> news:##Re07znJHA.1168@TK2MSFTNGP05.phx.gbl:
>>
>>> I have an array of int's.  I need to find an algorithm that computes
>>> the sum of every combination of numbers in the array.  So for
>>> instance if I had an array of 6 ints (lets say 5, 7, 10, 18, 24, and
>>> 33). I need to compute the sum of all possible combinations of these
>>> numbers.
>>>
>>> 5
>>> 5+7
>>> 5+7+10
>>> 5+7+10+18
>>> 5+7+10+18+24
>>> 5+7+10+18+24+33
>>> 5+10
>>> 5+10+18
>>> 5+10+18+24
>>> 5+10+18+24+33
>>>
>>> ad nauseum till I have computed the sum of every possible
>>> combination of the six numbers.  The plan is to stuff all of the
>>> possible results into a database table for additional processing
>>> later.
>>>
>>> Somewhere in the back of my six plus decade old brain there is a
>>> high school math teacher (probably long dead by now) saying "I told
>>> you that you would need this one day".  Shoulda' paid more
>>> attention!  Can any of you kind folks help me out.  Many thanks in
>>> advance.
>>>
>>> R Weiner
>>> www.WorksRite.com
>>
>> Interesting....there's probably a better way....but here's one.
>>
>> Six numbers.....think of as six bits. In counting for 1 to 63
>> (highest number 6 bits reperesents), each possible bit combination is
>> represented.
>>
>> You can assign the number to an array of 1 to 6, or 0 to 5, or
>> whatever.
>>
>> Then.....
>>
>> Dim x(5) As Long
>> Dim y As Long
>> Dim z As Long
>>
>> x(0) = 12
>> x(1) = 56
>> x(2) = 21
>> x(3) = 43
>> x(4) = 51
>> x(5) = 7
>>
>> Dim total As Long
>> For y = 1 To 63
>>   For z = 0 To 5
>>      'Compare each bit in the y number
>>       If (y And (2 ^ z)) = (2 ^ z) Then
>>           Debug.Print x(z) & " + ";
>>           total = total + x(z)
>>       End If
>>   Next
>>   Debug.Print "= " & total
>>   total = 0
>> Next
>>
>> .....or similar....thee's something wrong here though, as we were
>> taught that the number of group subsets was 2^(count of numbers). 2^6
>> = 64, so maybe there's something wrong above, but I'm sure you get
>> the gist of it.
>
>