Home |
Search |
Today's Posts |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
This recursive solution is very interesting, especially because of its
simplicity and relative efficiency. It is the most efficient if you really just want to generate strings. In the program I'm working on (a Kakuro generator/solver) I need bit pattern representations as well and convert bitpatterns to strings and ranges of numbers and vice versa, and also to know the number of digits / items in the pattern as well as the sum of all the digits. I adapted Andrews recursive routine to accomodate Silvias request and added my version of the sequential solution, which would be more efficient than the recursive one if the routines BitCount() and Bits2String() would be built-in or written in a more efficient programming language. Anybody any idea where I can find these? This would speed up my solver/generator a lot. Especially the BitCount() routine. Const PermString As String = "abcdefghij", _ MinPerm As Integer = 2, _ MaxPerm As Integer = 5 Private r As Integer 'Adapted recursive version Sub ShowCombinations(strPrefix As String, strMain As String) Dim strFirst As String, strRest As String If Len(strMain) = 0 Then If Len(strPrefix) = MinPerm And Len(strPrefix) <= MaxPerm Then Cells(r, 2) = strPrefix r = r + 1 End If Exit Sub End If strFirst = Left(strMain, 1) strRest = Mid(strMain, 2) ShowCombinations strPrefix & strFirst, strRest ShowCombinations strPrefix, strRest End Sub ' Sequential version Sub Combi(Total As Integer, Lo As Integer, Hi As Integer, Representation As String) Dim i As Long, cnt As Integer For i = 1 To 2 ^ Total cnt = BitCount(i) If cnt = Lo And cnt <= Hi Then Cells(r, 1) = Bits2String(i, Representation) r = r + 1 End If Next i End Sub Function BitCount(ByVal Pat As Long) As Integer BitCount = 0 While Pat If Pat And 1 Then BitCount = BitCount + 1 Pat = Int(Pat / 2) Wend End Function Function Bits2String(ByVal BitPat As Long, SourceString As String) As String Dim i As Integer Bits2String = "" i = 1 While BitPat If BitPat And 1 Then _ Bits2String = Bits2String & Mid(SourceString, i, 1) i = i + 1 BitPat = Int(BitPat / 2) Wend End Function |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Need to calculate percentage with some variables | Excel Worksheet Functions | |||
How do I calculate maximum with variables. | Excel Worksheet Functions | |||
combination charts: two variables in one series? | Charts and Charting in Excel | |||
Is there an easy way calculate 2 variables in conditional sum wiz. | Excel Worksheet Functions | |||
calculate the derivative (rate of change) of variables | Excel Programming |