2

So, I need to find the total number of combinations for a list of 10 data points that add up to AT LEAST 100. All of the examples to solve this I've found only give the number of combinations that EQUAL a number. How can I total the combinations that are higher than 100? Is this doable without VBA in Excel?

Another idea was calculating the total number of combinations (10!) and subtracting those UNDER 1,000 to find the number. But again, how to find the total number of combinations falling in a range?

Problem statement: 10 data points (65, 52, 48, 44, 42, 41, 39, 38, 30, 18) How many combinations can total at least 100?

Sources I've tried (I can't post more than 2 links) http://www.tushar-mehta.com/excel/templates/match_values/index.html

Find out which combinations of numbers in a set add up to a given total

A.S.H
  • 29,101
  • 5
  • 23
  • 50
Joe Johnson
  • 73
  • 1
  • 1
  • 10

2 Answers2

3
  1. Enter the 10 items in A1 through J1
  2. In K1 enter the formula =IF(ROW()<512,"0" & DEC2BIN(ROW(),9),"1" & DEC2BIN(ROW()-512,9))
  3. Copy row 1 down through row 1023
  4. Copy column K and PasteSpecialValues into column L
  5. Use TextToColumns to parse column L into columns M through V
  6. In W1 enter =SUMPRODUCT((A1:J1)*(M1:V1)) and copy down

Column W contains the sums of all possible combinations.

Finally, in another cell, enter:

=COUNTIF(W:W,">=" & 100)

enter image description here

EDIT#1:

To generate a 36 bit binary pattern, I use:

=DEC2BIN(INT(ROW()/2^27),9)&DEC2BIN(INT(MOD(ROW(),2^27)/2^18),9)&DEC2BIN(INT(MOD(ROW(),2^18)/2^9),9)&DEC2BIN(MOD(ROW(),2^9),9)

So for 20 bits, just use:

=RIGHT(DEC2BIN(INT(ROW()/2^27),9)&DEC2BIN(INT(MOD(ROW(),2^27)/2^18),9)&DEC2BIN(INT(MOD(ROW(),2^18)/2^9),9)&DEC2BIN(MOD(ROW(),2^9),9),20)



enter image description here

EDIT#2:

To do something like TextToColumns with VBA, select the cells and run this macro:

Sub PseudoTextToColumns()
    Dim r As Range, L As Long, i As Long, t As String
    For Each r In Selection
        t = r.Text
        L = Len(t)
        For i = 1 To L
            r.Offset(0, i).Value = Mid(t, i, 1)
        Next i
    Next r
End Sub

NOTE:

This will not work for 21 items.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
1

VBA Brute Force solution

I would share the following brute force solution that checks all possible combinations. It is fast enough on small arrays up to 15 entries or so. It solves your problem and you can take it as a model for any brute computation and also as a starting point to optimize and apply faster algorithms. It can also be a good basis for testing any optimized algorithm that you might implement later.

Option Explicit

' Main function. Computes the all combinations and prints those that exceed the target
' Each combination is specified by a mask of bits that specifies whether to take o to leave the entry
Function CombinationsByBruteForce(inputArray() As Double, target As Double) As Long
  Dim mask As Long, count As Long
  For mask = 1 To 2 ^ (1 + UBound(inputArray)) - 1
    If SumByMask(inputArray, mask) > target Then
      count = count + 1
      PrintByMask inputArray, mask
    End If
  Next
  Debug.Print "Total number of successful combinations: " & count
  CombinationsByBruteForce = count
End Function

' computes the sum for a given combination.  The combination is specified by a mask of bits
' that tells which entries belong to the combination (the 1 bits)
Function SumByMask(inputArray() As Double, mask As Long) As Double
  Dim bit As Long
  For bit = 0 To UBound(inputArray)
    If (mask And (2 ^ bit)) <> 0 Then SumByMask = SumByMask + inputArray(bit)
  Next
End Function

' Prints out the entries belonging to a combination specified by a mask
Sub PrintByMask(inputArray() As Double, mask As Long)
  Dim bit As Long
  For bit = 0 To UBound(inputArray)
    If (mask And (2 ^ bit)) <> 0 Then Debug.Print inputArray(bit),
  Next
  Debug.Print
End Sub

' For testing. An array of doubles and a target are specified.
' The variant array is copied into a Double array to improve speed
Sub testing()
  Dim target As Double: target = 350   ' target number to exceed
  Dim test: test = Array(65, 52, 48, 44, 42, 41, 39, 38, 30, 18)

  'convert to explicit array of Double
  ReDim inputArray(UBound(test)) As Double
  Dim i As Long
  For i = LBound(test) To UBound(test): inputArray(i) = test(i): Next

  ' Launsh the brute force computation
  CombinationsByBruteForce inputArray, target
End Sub

Output for the above test:

 65          52  48          44   42          41  39          38  
 65          52  48          44   42          41  39          30  
 65          52  48          44   42          41  38          30  
 65          52  48          44   42          39  38          30  
 65          52  48          44   41          39  38          30  
 65          52  48          42   41          39  38          30  
 65          52  44          42   41          39  38          30  
 65          52  48          44   42          41  39          38   30         
 65          52  48          44   42          41  39          38   18         
 65          52  48          44   42          41  39          30   18         
 65          52  48          44   42          41  38          30   18         
 65          52  48          44   42          39  38          30   18         
 65          52  48          44   41          39  38          30   18         
 65          52  48          42   41          39  38          30   18         
 65          52  44          42   41          39  38          30   18         
 65          48  44          42   41          39  38          30   18         
 52          48  44          42   41          39  38          30   18         
 65          52  48          44   42          41  39          38   30          18  
Total number of successful combinations: 18

As an entry point for solving with the solver, you can try a BIP model, where the decision variables are the bits of the mask. The solver should then apply some branch and bound or similar technique to find a combination. But to print out all the successful combinations with the solver seems much more difficult to solve with the solver, because it is not an optimization problem.

A.S.H
  • 29,101
  • 5
  • 23
  • 50