Return permutations of values that sum to a target - with constraints, repeats allowed

ech1982

New Member
Joined
Mar 26, 2018
Messages
6
I'm trying to produce a list of number sequences, 4 numbers long that sum to a target value of 32.
The constraints are that the numbers must be min = 0.5, max = 9. The numbers can only increment in 0.5.
That is to say, I have a list of numbers (0.5, 1, 1.5, 2, 2.5, 3, etc............9). I need a list that shows every possible order permutation from this list that adds to exactly 32. The same number can appear multiple times e.g. the sequence could be 8, 8, 8, 8 = 32 or 6.5, 7.5, 9, 9 = 32

I have seen similar examples where each number can used once i.e. to match outstanding bills a statement. The key difference here is that the numbers from the list can repeat as many times as possible.
Also, order does matter for my purpose. For example I need to report 6, 8, 9, 9, as being distinct from 9, 9, 8, 6 or 8, 9, 6, 9 etc.
I recognize this will be a very long list >73,000 permutations.
Any help would be greatly appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
In fact there are 165 permutations:

Code:
Sub test2()
Dim a As Double, b As Double, c As Double, d As Double, r As Long

    Application.ScreenUpdating = False
    For a = 5 To 9 Step 0.5
        For b = 5 To 9 Step 0.5
            For c = 5 To 9 Step 0.5
                For d = 5 To 9 Step 0.5
                    If a + b + c + d = 32 Then
                        r = r + 1
                        Cells(r, "A") = a
                        Cells(r, "B") = b
                        Cells(r, "C") = c
                        Cells(r, "D") = d
                    End If
                Next d
            Next c
        Next b
    Next a
    Application.ScreenUpdating = True

End Sub
It always puzzles me when someone asks for a huge list of permutations/combinations. There's never a good reason to list them.

Note that the macro start at 5 instead of .5, because 5 is the lowest value that can be used (32 - 9 - 9 - 9 = 5). If you change your target, you may need to change the boundaries on the For statements.
 
Upvote 0
Thanks for this solution and apologies for the oversight.
Purpose for creating the list is for proof of concept for a modelling project.
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top