Does anyone have an idea of how to take three letters a,b,c and then find all the possible combinations? Thanks
Printable View
Does anyone have an idea of how to take three letters a,b,c and then find all the possible combinations? Thanks
What are you doing? programming something? tell more
I'm sure there is a more elegant way, but this works.
Code:Dim intIndex1 As Integer
Dim intIndex2 As Integer
Dim intIndex3 As Integer
Const LETTERS = "abc"
For intIndex1 = 1 To 3
For intIndex2 = 1 To 3
For intIndex3 = 1 To 3
If intIndex1 = intIndex2 Or _
intIndex1 = intIndex3 Or _
intIndex2 = intIndex1 Or _
intIndex2 = intIndex3 Or _
intIndex3 = intIndex1 Or _
intIndex3 = intIndex2 Then
' Don't print because it will have duplicate letters
Else
Debug.Print Mid$(LETTERS, intIndex1, 1) & _
Mid$(LETTERS, intIndex2, 1) & _
Mid$(LETTERS, intIndex3, 1)
End If
Next
Next
Next
I like that one Martin, could you make one that handles n letters?
I just studied his code a bit and I beloieve thaat you just add on to intIndex#, and ad the necessary commads to make it function with 4.
Meg, that was not what i meant. I meant that you could put your letters in a dynamic array and then get a dynamic array out containing all combinations.
Looks good martin, if anyone was wondering I am doing this for a math probablility problem where I need to find all possible combinations.
it's easier just to work it out
what do you mean by the number of combinations of the letters a,b,c
do you mean the possible orders of the 3 letters
abc
acb
bac
bca
cab
cba
or if you allow repeats ie aab or aaa then it's 3^3 = 27
just stick your problem up.
But whewn you get into bigger numbers, it might be a difficult to do all of them mechanically.
this leads to some serious numbers. a whole alphabet's worth will return (6.15611958 x 10E36).
My scientific calculator packs up at 56^56 (7.9 x 10E97) !
This type of code can be used to crack passwords. It keeps trying every single letter combonation until it gets it right.
yes you could figure it mechically but that will get very confusing quickly as I found out. Im working on a soultion that I think will work. I will post it when I finish
post up the question, i'll put the answer, but you'll have to give it in a bit more detail than you did.
Thos works for any number of letters and any combination including strings like "abAcd".
[Deleted: See my updated post further down.]
[Edited by MartinLiss on 05-28-2000 at 08:03 AM]
Great work Martin! Thanks
You're welcome, but I needed to modify it because ints just don't do it when you can have millions of combinations.
Code:Dim dblFactorial As Double
Dim intLen As Integer
Dim intColumn As Integer
Dim intLetter As Integer
Dim dblRepeats As Double
Dim dblEntry As Double
Dim dblRepFactor As Double
Dim strCombos() As String
Dim bIsUsed() As Boolean
Const LETTERS = "abcde"
' Determine the number of letters
intLen = Len(LETTERS)
' Calculate the number of combinations of letters
' For 4 letters that would be 4 * 3 * 2 * 1 = 24
dblFactorial = intLen
Do Until intLen = 1
intLen = intLen - 1
dblFactorial = dblFactorial * intLen
Loop
' ...and dim the array to hold just that many
ReDim strCombos(dblFactorial - 1)
' Restore the number of letters since we decremented it in the factorial calc
intLen = Len(LETTERS)
' Dimension the table that will record which letters have been used in
' which locations, so that duplicates can be avoided
ReDim bIsUsed(dblFactorial - 1, intLen - 1)
' Set an initial value for the letter repeat factor
dblRepFactor = dblFactorial / intLen
' Build the "columns" vertically
For intColumn = 0 To intLen - 1
intLetter = 1 ' When the column changes, start the letters over again
For dblEntry = 0 To dblFactorial - 1
Do While bIsUsed(dblEntry, intLetter - 1)
' The pending letter is already in the combo so try the next one...
intLetter = intLetter + 1
If intLetter > intLen Then
' ...but don't go "off the end" of the letters
intLetter = 1
End If
Loop
' OK, we've got a valid entry so record it
strCombos(dblEntry) = strCombos(dblEntry) & Mid$(LETTERS, intLetter, 1)
bIsUsed(dblEntry, intLetter - 1) = True
' Keep track of how many times a given letter has been repeated...
dblRepeats = dblRepeats + 1
If dblRepeats = dblRepFactor Then
' ...and when it's at its max, start over again
dblRepeats = 0
intLetter = intLetter + 1
If intLetter > intLen Then
intLetter = 1
End If
End If
Next
' Recalculate the repeat factor for the next "column"
If dblRepFactor > 1 Then
dblRepFactor = dblRepFactor / (intLen - (intColumn + 1))
End If
Next
For dblEntry = 0 To UBound(strCombos)
Debug.Print strCombos(intLetter)
Next
A similar question was posted a few months ago. It got me interested in writing a VB version of a Mainframe program I wrote many years ago.
My VB application provides a text box into which a string is entered. Command Buttons can be used to generate permutations of the bytes in the string (either all at once or one at a time). The permuations are put into a List Box.
If anyone is interested, I will send the VB Files used by my application (I have not deployed the application). I am running VB 6 under windows 98.
The algorithm appeared in Scientific American about 20 years ago, and is considered the best possible method. I do not have a good description of the algorithm, and do not want to create one (it is easier to show examples than to describe how to do it). The VB application makes the approach obvious.
Yes please,
I've been coding up the DES spec and my coding of the permutations if slowing everything. It works but its too slow for large files, I'd like to see some better permutation algorithms to get some better ideas.
[email protected]