Select m elements from max elements with limits

I have max integers between 1 and max, If I choose m of them to get a sum sums ,Suppose I have counts ways,How can I get the counts ?

I write some codes by recursion

------------------------------------

Function counts(ByVal max As Long, ByVal m As Long, ByVal sums As Long) As Long
Dim temp As Long, i As Long, j As Long
If max >= m And m = 1 Then counts = IIf(sums <= max, 1, 0)
If max >= m And m = 2 Then
For i = 1 To max - 1
For j = i + 1 To max
If i + j = sums Then counts = counts + 1
Next
Next
End If
If max >= m And m > 2 Then counts = counts(max - 1, m, sums - m) + counts(max - 1, m - 1, sums - m)
End Function

Sub xxx()
MsgBox counts(49, 6, 101)
End Sub

Return 58446

你可能感兴趣的:(J#)