View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
TwIsTeR TwIsTeR is offline
external usenet poster
 
Posts: 3
Default Count combinations

TwIsTeEeR wrote:
From a set of integers from 1 to 49,
take 6 unique integers,
that if we sum them up, this sum is equal to 150.

Conditions:
1. The set numbers are integers from 1 to 49
2. subset size is 6
3. sum of the selected subset numbers is 150

My questions:
A. How many sets (combinations) of 6 unique numbers exist that their sum
is 150?
B. Do you know of a function that can calculate that quantity
of combinations, for different values of conditions (1), (2), & (3)?

Thank you,


The code below satisfies the first part of the question.
Is it possible to convert this code to a recursive function,
so that the term SetSize can be satisfied also?

Thanks,


Sub Test()


Dim minval As Integer
Dim maxval As Integer
Dim Target As Long
Dim sum As Long
Dim Cnt As Long
Dim SetSize As Integer
Dim RetVals As String



minval = 1
maxval = 49
SetSize = 6
Target = 150
sum = 0

For I = minval To (Target + 5) / 6 - 3
sum = sum + I
For J = I + 1 To (Target + 4 - sum) / 5 - 2
sum = sum + J
For K = J + 1 To (Target + 3 - sum) / 4 - 2
sum = sum + K
For L = K + 1 To (Target + 2 - sum) / 3 - 1
sum = sum + L
For M = L + 1 To (Target + 1 - sum) / 2 - 1
N = Target - sum - M
If (N <= maxval) Then
Cnt = Cnt + 1
End If
Next
sum = sum - L
Next
sum = sum - K
Next
sum = sum - J
Next
sum = sum - I
Next

RetVals = MsgBox("When " & vbTab & "MinN=" & minval & _
vbNewLine & " " & vbTab & "MaxN=" &
maxval & _
vbNewLine & " " & vbTab & "Set size=" &
SetSize & _
vbNewLine & " " & vbTab & "Sum=" & Target &
vbNewLine & _
"There are = " & Cnt & " Combinations to satisfy
the condition." & vbNewLine & " ", 64, "Combinations...")
Select Case RetVals
Case 1: 'OK
End Select

End Sub