Results 1 to 7 of 7

Thread: Permutations

  1. #1

    Thread Starter
    Addicted Member Jez1's Avatar
    Join Date
    Oct 2001
    Location
    Warwickshire, England
    Posts
    185

    Permutations

    Here is some code I got a while ago, to tell me all the different combinations of 1-9, then save to a txt file.

    Code:
    Option Explicit
    
    'Must Declare all variables, I think that it is good practice
    Option Base 1 'For simplicity, make array index start at 1 To n
    
    
    Sub Perm(A() As String * 1, m, n As Integer)
        Dim i As Integer
        Dim Temp As String * 1
    
        If m = 1 Then
            PrintPerm A, n 'Print out the permutations
        Else
            For i = 1 To m
                Temp = A(i)
                A(i) = A(m) 'Exchange A(i) and A(m), could have made a Sub To Do this
                A(m) = Temp
                Perm A, m - 1, n 'Recursive Function call
                Temp = A(m)
                A(m) = A(i) 'Exchange A(m) and A(i)
                A(i) = Temp
            Next i
        End If
    End Sub
    
    
    Private Sub Command1_Click()
        Dim strArray(9) As String * 1
        Dim x As Long
        
        'create array of values 1..9
        For x = 1 To 9
            strArray(x) = Trim(CStr(x))
        Next x
        Perm strArray(), 6, 6 'Of course you could use variables For this
    End Sub
    
    
    Public Sub PrintPerm(A() As String * 1, n As Integer)
        Dim i As Integer
        Dim strVar As String
    
        For i = 1 To n
            strVar = strVar & (A(i))
        Next i
    
    Dim combspath
    combspath = "C:\permutations.txt"
    Dim filenum As Integer
    filenum = FreeFile
    Open combspath For Append As #filenum
    Print #filenum, strVar
    Close #filenum
    Text1.Text = strVar
    End Sub
    It all works fine, but I don't really understand it, and now I need just numbers 1-6 combinations.

    Can anyone tell me how to modify so it does that?

    Thanks

  2. #2
    Addicted Member
    Join Date
    Feb 2001
    Posts
    198
    Looks like you've already done that.
    In the line:

    Perm strArray(), 6, 6 'Of course you could use variables For this

    I imagine the 6's were 9's when you got it ?

  3. #3

    Thread Starter
    Addicted Member Jez1's Avatar
    Join Date
    Oct 2001
    Location
    Warwickshire, England
    Posts
    185

    ...

    Hmm. That's what I thought, but it doesn't seem to produce anywhere near enough permutations -
    I'm no mathamatician but I guess there's more than 750, which is what this gives me.

  4. #4
    Addicted Member
    Join Date
    Feb 2001
    Posts
    198
    720

    number 1 can appear in any of the 6 places
    number 2 can appear in any of the other 5 places
    number 3 can appear in any of the other 4 places
    etc.

    so total number is 6x5x4x3x2x1 = 720

  5. #5

    Thread Starter
    Addicted Member Jez1's Avatar
    Join Date
    Oct 2001
    Location
    Warwickshire, England
    Posts
    185

    Thanks

    So there really is only 750 permutations of the numbers 1-6?

    Wow, thanks.

  6. #6
    Addicted Member
    Join Date
    Aug 2002
    Location
    Windsor, Ontario's City of Pollution
    Posts
    165
    There are 6! permutations, because of what Starman said:

    "number 1 can appear in any of the 6 places
    number 2 can appear in any of the other 5 places
    number 3 can appear in any of the other 4 places
    etc.

    so total number is 6x5x4x3x2x1 = 720"

    If you have a permutation of n distinct objects (doesn't have to be numbers), it would be n! or n*(n-1)*(n-2)*(n-3)*...*3*2*1
    Merry Math Making!

  7. #7
    Addicted Member
    Join Date
    Aug 2002
    Location
    London UK
    Posts
    255
    While we're talking about permutations, I think you should all go out and buy Amon Tobin - Permutation. A VERY classy album indeed, experimental drum 'n' bass at its best.
    Not at all related to sheep...

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