97 - 1 - 1 - 1
...
and so on till 1 - 1 - 1 - 97
...
(not permutations)
1-1-1-97 is a permutation of 97-1-1-1 so this routine would return only one of them, not both.
Perhaps this will do what you want.
I also think that it might be wise for you (or one of the mods) to remove the email address from your post.
Note that the shape of ElementsSummingTo(100,4) is 4 rows by 7153 columns.
Sub test()
Dim myAns As Variant
myAns = ElementsSummingTo(100, 4)
Range("A1").Resize(UBound(myAns, 2), UBound(myAns, 1)).Value = Application.Transpose(myAns)
End Sub
Function ElementsSummingTo(ByVal SumOfElements As Long, ByVal NumberOfElements As Long) As Variant
Dim Result As Variant
Dim subResult As Variant
Dim CurrentHigh As Long
Dim flag As Boolean
Dim i As Long, j As Long, Pointer As Long
ReDim Result(1 To NumberOfElements, 1 To 1)
If NumberOfElements = 1 Then
Result(1, 1) = SumOfElements
Else
CurrentHigh = SumOfElements - (NumberOfElements - 1)
Pointer = 0
Do
subResult = ElementsSummingTo(SumOfElements - CurrentHigh, NumberOfElements - 1)
flag = False
For i = 1 To UBound(subResult, 2)
If subResult(1, i) <= CurrentHigh Then
flag = True
Pointer = Pointer + 1
If UBound(Result, 2) < Pointer Then ReDim Preserve Result(1 To NumberOfElements, 1 To 2 * Pointer)
Result(1, Pointer) = CurrentHigh
For j = 2 To NumberOfElements
Result(j, Pointer) = subResult(j - 1, i)
Next j
End If
Next i
CurrentHigh = CurrentHigh - 1
Loop While flag
ReDim Preserve Result(1 To NumberOfElements, 1 To Pointer)
End If
ElementsSummingTo = Result
End Function
Bookmarks