# Factorial problem.

I have this collection:

A,B,C,D,E

How can i find all possibilities between this collection? I know that 5 factorial=120 (this is the number of possibilities).

A,B,C,D,E
A,B,C,E,D
A,B,D,C,E
...

Can any one show me sme code to implement this.
I want to retrieve a collectionwith all possibilities.

Note: The initial collection can change from 5 to any number of elements.

Thanks

Carlos Rodrigues
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
progCommented:
Option Explicit

Dim colX As New Collection

Dim v, c

Combinations "ABCDE", colX

For Each v In colX
c = c + 1
Debug.Print c, v
Next

End Sub

Private Function Combinations(x As String, col As Collection)

Dim sTest As String
Dim c As Long
Dim colX As Collection
Dim sString

If Len(x) > 1 Then

For c = 1 To Len(x)

sTest = Left(x, c - 1) & Mid(x, c + 1)

Set colX = New Collection

Combinations sTest, colX

For Each sString In colX

col.Add Mid(x, c, 1) & sString

Next

Set colX = Nothing

Next

Else

End If

End Function

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Here is a non-recursive way:

Option Explicit

Dim colFac As New Collection
Dim DaRest As String

Dim s As Variant
Dim j As Integer, k As Integer

' seed the collection
DaRest = "CDE"

' generate combinations
For j = 1 To Len(DaRest)
For Each s In colFac
If Len(s) = j + 1 Then
colFac.Remove s
s = s + Mid(DaRest, j, 1)
For k = 1 To j + 1
s = Right(s, j + 1) + Left(s, 1)
Next k
End If
DoEvents ' allows breaking out early
Next s
Next j

' display results
For Each s In colFac
Next s

Caption = "Item Count = " + CStr(colFac.Count)
End Sub
Commented:
Hi Carlos_Rodrigues,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

Accept deighton's comment(s) as an answer.

Carlos_Rodrigues, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Commented:
Per recommendation, force-accepted.

Netminder
CS Moderator
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.