Results 1 to 5 of 5

Thread: Perm 3 from X

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Perm 3 from X

    I am trying to get all different unique permutations of 2 numbers from X numbers
    I then create folders based on the result.

    I've made a fundamental error in the code below as it generates 234 and 243 which are (in this case) the same.

    VB Code:
    1. Dim intItems As Integer
    2. Dim strCalcID As String
    3. Dim i As Integer
    4. Dim j As Integer
    5.  
    6. intItems = 6
    7.  
    8. For i = 1 To intItems - 2
    9.  
    10.     For j = 2 To intItems - 1
    11.         If j = i Then
    12.             j = j + 1
    13.         End If
    14.  
    15.             For k = 3 To intItems
    16.                 If k = i Or k = j Then
    17.                     k = k + 1
    18.                 End If
    19.  
    20.                 'Write which items are used to variable
    21.                 strCalcID = CStr(i & j & k)
    22.                
    23.                 'Check if directory exists
    24.                 If Dir(strCalcID & "\") = "" Then
    25.                     'Create subdirectory based upon item identifiers
    26.                     fso.CreateFolder (strCalcID)
    27.                 End If
    28.        
    29.             Next
    30.     Next
    31. Next

  2. #2
    Lively Member
    Join Date
    Jan 2003
    Posts
    79
    Hello agmorgan,
    Just a couple of things I am not clear on.

    1) You are not getting all the possible permutations i.e. you miss numbers like 111, any particular reason?

    2)Why do you say that "234 and 243 which are (in this case) the same"? How are they the same?

    As far as I have tested it your code seems to be technically OK (it runs without errors).

    All the best,

    Dave.

  3. #3

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383
    It is used to select items from a list.
    So as I need 3 separate items, I can skip things like 111 and 222 as they only include 1 item in each
    234 and 243 are the same as they contain the same numbers

    I think what I am trying to say is that the order of the numbers isnt significant.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    why don't you explain what you're trying to do. there is prolly a better way to be doing it. if we know what you're doing, then we can offer suggestions. I don't understand.

  5. #5
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527
    Hi there, here is a modified simplified version of the routines I use for calculating permutations.

    Start a new project, add a Command Button to it and then paste this code into the form module. You should be able to work out the rest from there (ask if you need more help).

    VB Code:
    1. Option Explicit
    2.  
    3. Private Row() As String
    4.  
    5. Private Sub Command1_Click()
    6.     CalculatePerms 5, 3
    7. End Sub
    8.  
    9. Private Sub CalculatePerms(ByRef NoOfItems As Long, ByRef ItemsPerPerm As Long)
    10.  
    11.     ReDim Row(ItemsPerPerm - 1)
    12.     Combinations 0, NoOfItems, 0, ItemsPerPerm - 1
    13.  
    14. End Sub
    15.  
    16. Private Sub Combinations(Start As Long, NoOfItems As Long, NodeIndex As Long, ItemsPerPerm As Long)
    17.  
    18.     Dim Index As Long
    19.  
    20.     If NodeIndex > ItemsPerPerm Then
    21.         'at this point here, the Row array holds the indexes of the items in this combination
    22.         MsgBox Join$(Row, ",")
    23.     Else
    24.         For Index = Start To NoOfItems
    25.             Row(NodeIndex) = "Item: " & CStr(Index)
    26.             'recursively generate combinations from i+1...n
    27.             Call Combinations(Index + 1, NoOfItems, NodeIndex + 1, ItemsPerPerm)
    28.         Next
    29.     End If
    30.  
    31. End Sub

    Blade

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width