Creating character combinations out of a possible set...

S

Shaka215

Hey! Here is the problem...I can't figure out how to make it so every
possible combination of a series of letters (A-Z) and Numbers (0-9) is
displayed I saw someones code to make it possible for three characters
but I am looking to do it with like 10 characters...The code I saw was
this...

Sub comboo()
Dim s(36) As String
For i = 0 To 9
s(i) = Chr(i + 48)
Next
For i = 10 To 36
s(i) = Chr(i + 55)
Next


k = 1
For i = 0 To 35
For j = 0 To 35
For l = 0 To 35
Cells(k, "A").Value = s(i) & s(j) & s(l)
k = k + 1
Next
Next
Next
End Sub

Any ideas? Any help is much appreciated!!
 
B

Bernard Liengme

With 26 letters and 10 digits you have 36 possible characters in each
position
So for 10 positions you will have 36^10 possible outcomes
This is about 4E+15 or a mere 1/1000 the age of the universe expressed in
seconds
Hope you have a fast CPU
And a big piece of paper on which to print the results
best wishes
 
K

Ken Johnson

Where are you intending to display them?

There are 36^10 of them, which is more than three million,six hundred
and fifty-six thousand,one hundred and sixty billion!

Ken Johnson
 
D

Dana DeLouis

...every possible combination is displayed.

Hi. As others have mention, the output is too large.
A newer measure of large might be that you would have to completely fill
3,486,784,401 Columns in Excel 2007. (36^10/2^20) <vbg>
In Combinations, one is usually not interested in generating all
combinations. One is usually interested in generating only a few of the
outputs. One might be interested in the 1 millionth item in the list, or
perhaps pick a few items from the list at Random.
It is usually more efficient to generate the combinations as needed. The
function name usually goes by the name UnRank...(ie UnRankCombinations, or
something similar for your example)

The code below just demo's how this might be done for your 3 character
example.
Excel vba can easily find the 2456158341262976 th item from your 10
character example, but it requires a little modification.

Again, this is just a simple example...

Sub TestIt()
Debug.Print UnRank(1) 'First item in list
Debug.Print UnRank(46656)'Last item in list
Debug.Print UnRank(1372)' the 1372 th item in list
End Sub

Returns:
000
ZZZ
123

Function UnRank(Num As Double) As String
Dim n As Double
Dim Block As Double
Dim j As Long
Dim M As Double
Dim s(0 To 35) As String
Const k As Double = 36

For n = 0 To 35
s(n) = Chr((n + 48) - 7 * (n > 9))
Next n

'// Note: First item is 000
n = Num - 1
Block = k ^ 2

For j = 2 To 0 Step -1
M = Int(n / Block)
UnRank = UnRank & s(M)
n = n - M * Block
Block = Block / k
Next j
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top