Permutation or Combination

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
530
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Thanks in advance

I want to know that from the given

(Query-1) numbers 0,1,2,3,4,5,6,7
(Query-2) numbers 1,2,3,4,5,6,7,8

how many combination we can generate without repetition and if possible pls provide the generated combination.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hey,

By combinations do you mean 0,1,2,3,4,5,6,7 is 1 combination, 0,1,2,3,4,5,7,6 would be another?

If so then for both queries there are 40320 combinations (8!)
 
Upvote 0
I have a permute function which i could shoehorn into a solution for you. try:
Code:
Sub combinations()
    Dim numbers, output(), v
    Dim d As Object
    Dim i As Long, j As Long
    Dim s As String
    
    numbers = "1,2,3,4,5,6,7,8"
    numbers = Split(numbers, ",")
    Set d = permute(UBound(numbers) + 1)
    ReDim output(1 To d.count, 1 To 1)
    i = 0
    For Each v In d.items
        i = i + 1
        s = ""
        For j = 1 To UBound(v)
            s = s & IIf(s = "", "", ",") & numbers(v(j) - 1)
        Next j
        output(i, 1) = s
    Next v
    Workbooks.Add.Sheets(1).Range("A1").Resize(UBound(output), UBound(output, 2)).Value = output
    Set d = Nothing
    Erase output
End Sub
    
Public Function permute(n As Integer) As Object
    Dim P() As Integer, permSet() As Integer
    Dim t As Integer, i As Integer, j As Integer, k As Integer
    Dim count As Long
    Dim Last As Boolean
    Dim d As Object
    
    If n <= 1 Then
        Debug.Print "Please give a number greater than 1"
        Exit Function
    End If
    'Initialize
    Set d = CreateObject("Scripting.Dictionary")
    ReDim P(n)
    For i = 1 To n
        P(i) = i
    Next i
    count = 0
    Last = False
    Do While Not Last
        ReDim permSet(n)
        For t = 1 To n
            permSet(t) = P(t)
            'Debug.Print P(t);
        Next
        'Debug.Print
        count = count + 1
        d.Add count, permSet
        Last = True
        i = n - 1
        Do While i > 0
            If P(i) < P(i + 1) Then
                Last = False
                Exit Do
            End If
            i = i - 1
        Loop
        j = i + 1
        k = n
        While j < k
            ' Swap p(j) and p(k)
            t = P(j)
            P(j) = P(k)
            P(k) = t
            j = j + 1
            k = k - 1
        Wend
        j = n
        While P(j) > P(i)
            j = j - 1
        Wend
        j = j + 1
        'Swap p(i) and p(j)
        t = P(i)
        P(i) = P(j)
        P(j) = t
    Loop 'While not last
    Debug.Print "Number of permutations: "; count
    Set permute = d
End Function
 
Upvote 0
If you mean combinations, there are 8:

7 6 5 4 3 2 1 0
8 6 5 4 3 2 1 0
8 7 5 4 3 2 1 0
8 7 6 4 3 2 1 0
8 7 6 5 3 2 1 0
8 7 6 5 4 2 1 0
8 7 6 5 4 3 1 0
8 7 6 5 4 3 2 0
8 7 6 5 4 3 2 1
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top