I have read all of the past posts but did not find any that were directly related to what I am trying to do here, save maybe one or two.
Start with 2 numbers, we'll say 15 and 23 for now but they will be variable in the actuall application. I want to find all combinations of a 23 bit binary word that have exactly 15 1's. I know from the code below that there are 170,544 of them. However, I was wondering if there is a better or more efficient way to do this than my attempt as shown. It takes approx. a minute and a half, which isn't too bad considering there are over 8,000,000 combinations.
Code:Private Sub SuggestNewWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles SuggestNewWorker.DoWork Try Dim fdrMax As Integer = 15 Dim fdrInv As Integer = 23 Dim combos As Integer = 0 Dim binWord As String = “” Dim foundHighs As Integer = 0 Dim starter As Int64 = 2 ^ (fdrMax – 1) Dim stopper As Int64 = 2 ^ (fdrInv – 1) Dim volume As Int64 = stopper – starter Dim progMark As Integer = CInt(stopper \ 100) Dim progCheck As Integer = 0 Dim progRep As Integer = 0 For runner As Int64 = starter To stopper Step 1 For cnt As Integer = 1 To fdrInv If runner And (2 ^ (cnt – 1)) Then binWord = “1” & binWord foundHighs += 1 Else binWord = “0” & binWord End If Next cnt If foundHighs = fdrMax Then combos += 1 End If If progCheck = progMark Then progRep += 1 Me.SuggestNewWorker.ReportProgress(progRep) progCheck = 0 Else progCheck += 1 End If binWord = “” foundHighs = 0 Application.DoEvents() Next Runner Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub




Reply With Quote