LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default calculate all possible combination from 10 variables

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to calculate percentage with some variables Cristin Excel Worksheet Functions 4 September 23rd 08 02:02 AM
How do I calculate maximum with variables. DanASU Excel Worksheet Functions 3 June 13th 08 08:01 PM
combination charts: two variables in one series? Bob with Q Charts and Charting in Excel 1 September 14th 06 09:55 AM
Is there an easy way calculate 2 variables in conditional sum wiz. Liketoknow Excel Worksheet Functions 1 November 10th 04 09:34 AM
calculate the derivative (rate of change) of variables Fyon Excel Programming 2 January 29th 04 03:56 PM


All times are GMT +1. The time now is 04:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"