Fonction factorielle

steph71

XLDnaute Occasionnel
Bonjour à tous

Existe t il dans EXCEL une fonction factorielle permettant de déterminer toutes les combinaisons possibles de 3 éléments avec 10 éléments ?

Merci d'avance
Bonne journée
 

Abel

XLDnaute Accro
Bonjour Steph71,

La fonction factorielle dans la feuille de calcul est '=FACT(nombre)'.

Pour le dénombrement de 'p' parmis 'n' éléments, ce serait :

= FACT(n)/FACT(n-p)

On peux bien sur remplacer n et p par des adresses de cellule.


En espérant que cela te dépanne.

Abel.
 

Abel

XLDnaute Accro
Re,

Heu ... je crois qu'il y a une petite erreur dans ma formule. Je crois que c'est plutôt :

Combinaison p parmis n (peux pas faire Cnp) = n! / (p!(n-p)!)

Dans le fil joint ci-dessus en adaptant, ça ferait :

Code:
Sub combin()
Dim a1, a2, a3, a4, a5, a6
Dim z
z = 1
For i = 1 To 10
    a1 = Range('a' & i).Value
    For j = 2 To 10
        If i >= j Then
            GoTo a
            Else
            a2 = Range('a' & j).Value
            End If
        For k = 3 To 10
            If j >= k Then
                GoTo b
                Else
                a3 = Range('a' & k).Value
                Range('b' & z).Value = a1 & a2 & a3
                z = z + 1
                End If
b:
        Next
a:
    Next
Next
End Sub

Et puis si ma formule est juste et que les piles de ma calculette sont en état, 3 parmis 10 cela fait 120 solutions, non ?


Abel.
 

Abel

XLDnaute Accro
Re,

Oups !

Pardon Sylvie, pas vu.

Je pense que tu as raison. Le nombre de permutations possibles donne 2730.

Les permutations et les combinaisons ne sont pas la même chose.

Les plus calés que moi en proba et dénombrement répondront mieux.
Sylvie ?

Abel.
 

Abel

XLDnaute Accro
Re,

Ben adapte le code.

3 parmis 10, ça fait 720 solutions.
3 parmis 15, ça fait 2730 solutions.

Code:
Sub combin()
Dim a1, a2, a3
Dim z
z = 1
For i = 1 To 10
    a1 = Range('a' & i).Value
    For j = 1 To 10
        If i = j Then
            GoTo a
            Else
            a2 = Range('a' & j).Value
            End If
        For k = 1 To 10
            If j = k Or i = k Then
                 GoTo b
                Else
                 a3 = Range('a' & k).Value
                 Range('b' & z).Value = a1 & a2 & a3
                z = z + 1
                End If
b:
        Next
a:
    Next
Next
End Sub


En espérant que cela te dépanne.


Abel.

Edition : au fait, ma première formule concernait bien les permutations et non les combinaisons.

ps : je vois qu'il y a des ';' qui se baladent dans le code. Enlève les.

Message édité par: Abel, à: 24/08/2005 17:26
 

Gael

XLDnaute Barbatruc
Bonsoir à tous,

Il existe une fonction COMBIN dans XL qui fais ce calcul sans problème.

Les permutations comportent la notion d'ordre qu'il n'y a pas dans les combinaisons. Il y a donc plus de permutations que de combinaisons pour un même nombre d'éléments.

Pour mon 500ème, j'ai fait un arbre binaire avec l'exemple du loto qui vous donnera quelques exemples de probabilités mais je n'ai pas utilisé la fonction combin.

Lien supprimé

Il y a également une procédure très simple à utiliser qui est sur le site Excelabo et permet d'afficher tous les résultats. Je l'ai essayée ça marche super bien avec des chiffres ou du texte:

avec la fonction COMBIN (x;y) on peut calculer le nombre de combinaisons possibles. Mais comment faire pour afficher ou calculer toutes les combinaisons et toutes les permutations ?

Un ensemble de procédures de Myrna Larson permet de faire aussi bien la liste des combinaisons que des permutations (malgré le nom de la procédure principale 'ListPermutations' qui pourrait laisser croire qu'elle laisse les combinaisons de côté).
Ci-dessous le code, avec mode d'emploi, à recopier dans un module standard.

'Voici une diabolique procédure pour mettre
'définitivement fin aux questions concernant les
'listes de combinaisons ou de permutations
'de R éléments choisis parmi N.
'Pour l [/b]'utiliser :
 
'1. En A1, écrire c ou p ; (Combinaison ou Permutation)
 
'2. En A2, écrire la valeur de R ;
 
'3. Sous A2, écrire la liste des N éléments ;
 
'4. Sélectionner A1 et activer la procédure.

'Exemple:
'A1 c
'A2 3
'A3 1
'A4 2
'A5 Excel
'A6 4
'A7 *
'A8 6
'
'La procédure donne alors la liste de toutes les combinaisons
'possibles de 3 éléments choisis parmi 6.


Option Explicit

Dim vAllItems As Variant
Dim Buffer() As String
Dim BufferPtr As Long
Dim Results As Worksheet

Sub ListPermutations()
 
Dim Rng As Range
 
Dim PopSize As Integer
 
Dim SetSize As Integer
 
Dim Which As String
 
Dim N As Double
 
Const BufferSize As Long = 4096

 
Set Rng = Selection.Columns(1).Cells
 
If Rng.Cells.Count = 1 Then
   
Set Rng = Range(Rng, Rng.End(xlDown))
 
End If

  PopSize = Rng.Cells.Count - 2
 
If PopSize < 2 Then GoTo DataError

&nbsp; SetSize = Rng.Cells(2).Value
&nbsp;
If SetSize > PopSize Then GoTo DataError

&nbsp; Which = UCase$(Rng.Cells(1).Value)
&nbsp;
Select Case Which
&nbsp;
Case 'C'
&nbsp; &nbsp; N = Application.WorksheetFunction.Combin(PopSize, SetSize)
&nbsp;
Case 'P'
&nbsp; &nbsp; N = Application.WorksheetFunction.Permut(PopSize, SetSize)
&nbsp;
Case Else
&nbsp; &nbsp;
GoTo DataError
&nbsp;
End Select
&nbsp;
If N > Cells.Count Then GoTo DataError

&nbsp; Application.ScreenUpdating =
False

&nbsp;
Set Results = Worksheets.Add

&nbsp; vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value
&nbsp;
ReDim Buffer(1 To BufferSize) As String
&nbsp; BufferPtr = 0

&nbsp;
If Which = 'C' Then
&nbsp; &nbsp; AddCombination PopSize, SetSize
&nbsp;
Else
&nbsp; &nbsp; AddPermutation PopSize, SetSize
&nbsp;
End If
&nbsp; vAllItems = 0

&nbsp; Application.ScreenUpdating =
True
&nbsp;
Exit Sub

DataError:
&nbsp;
If N = 0 Then
&nbsp; &nbsp; Which = 'Enter your data in a vertical range of at least 4 cells. ' _
&nbsp; &nbsp; &nbsp; & String$(2, 10) _
&nbsp; &nbsp; &nbsp; & 'Top cell must contain the letter C or P, 2nd cell is the number' _
&nbsp; &nbsp; &nbsp; & 'of items in a subset, the cells below are the values from which' _
&nbsp; &nbsp; &nbsp; & 'the subset is to be chosen.'
&nbsp;
Else
&nbsp; &nbsp; Which = 'This requires ' & Format$(N, '#,##0') & _
&nbsp; &nbsp; &nbsp; ' cells, more than are available on the worksheet!'
&nbsp;
End If
&nbsp; MsgBox Which, vbOKOnly, 'DATA ERROR'
&nbsp;
Exit Sub
End Sub

Private Sub AddPermutation(Optional PopSize As Integer = 0, _
&nbsp;
Optional SetSize As Integer = 0, _
&nbsp;
Optional NextMember As Integer = 0)

&nbsp;
Static iPopSize As Integer
&nbsp;
Static iSetSize As Integer
&nbsp;
Static SetMembers() As Integer
&nbsp;
Static Used() As Integer
&nbsp;
Dim i As Integer

&nbsp;
If PopSize <> 0 Then
&nbsp; &nbsp; iPopSize = PopSize
&nbsp; &nbsp; iSetSize = SetSize
&nbsp; &nbsp;
ReDim SetMembers(1 To iSetSize) As Integer
&nbsp; &nbsp;
ReDim Used(1 To iPopSize) As Integer
&nbsp; &nbsp; NextMember = 1
&nbsp;
End If

&nbsp;
For i = 1 To iPopSize
&nbsp; &nbsp;
If Used(i) = 0 Then
&nbsp; &nbsp; &nbsp; SetMembers(NextMember) = i
&nbsp; &nbsp; &nbsp;
If NextMember <> iSetSize Then
&nbsp; &nbsp; &nbsp; &nbsp; Used(i) =
True
&nbsp; &nbsp; &nbsp; &nbsp; AddPermutation , , NextMember + 1
&nbsp; &nbsp; &nbsp; &nbsp; Used(i) =
False
&nbsp; &nbsp; &nbsp;
Else
&nbsp; &nbsp; &nbsp; &nbsp; SavePermutation SetMembers()
&nbsp; &nbsp; &nbsp;
End If
&nbsp; &nbsp;
End If
&nbsp;
Next i

&nbsp;
If NextMember = 1 Then
&nbsp; &nbsp; SavePermutation SetMembers(),
True
&nbsp; &nbsp; Erase SetMembers
&nbsp; &nbsp; Erase Used
&nbsp;
End If

End Sub&nbsp; 'AddPermutation

Private Sub AddCombination(Optional PopSize As Integer = 0, _
&nbsp;
Optional SetSize As Integer = 0, _
&nbsp;
Optional NextMember As Integer = 0, _
&nbsp;
Optional NextItem As Integer = 0)

&nbsp;
Static iPopSize As Integer
&nbsp;
Static iSetSize As Integer
&nbsp;
Static SetMembers() As Integer
&nbsp;
Dim i As Integer

&nbsp;
If PopSize <> 0 Then
&nbsp; &nbsp; iPopSize = PopSize
&nbsp; &nbsp; iSetSize = SetSize
&nbsp; &nbsp;
ReDim SetMembers(1 To iSetSize) As Integer
&nbsp; &nbsp; NextMember = 1
&nbsp; &nbsp; NextItem = 1
&nbsp;
End If

&nbsp;
For i = NextItem To iPopSize
&nbsp; &nbsp; SetMembers(NextMember) = i
&nbsp; &nbsp;
If NextMember <> iSetSize Then
&nbsp; &nbsp; &nbsp; AddCombination , , NextMember + 1, i + 1
&nbsp; &nbsp;
Else
&nbsp; &nbsp; &nbsp; SavePermutation SetMembers()
&nbsp; &nbsp;
End If
&nbsp;
Next i

&nbsp;
If NextMember = 1 Then
&nbsp; &nbsp; SavePermutation SetMembers(),
True
&nbsp; &nbsp; Erase SetMembers
&nbsp;
End If

End Sub&nbsp; 'AddCombination

Private Sub SavePermutation(ItemsChosen() As Integer, _
&nbsp;
Optional FlushBuffer As Boolean = False)

&nbsp;
Dim i As Integer, sValue As String
&nbsp;
Static RowNum As Long, ColNum As Long

&nbsp;
If RowNum = 0 Then RowNum = 1
&nbsp;
If ColNum = 0 Then ColNum = 1

&nbsp;
If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then
&nbsp; &nbsp;
If BufferPtr > 0 Then
&nbsp; &nbsp; &nbsp;
If (RowNum + BufferPtr - 1) > Rows.Count Then
&nbsp; &nbsp; &nbsp; &nbsp; RowNum = 1
&nbsp; &nbsp; &nbsp; &nbsp; ColNum = ColNum + 1
&nbsp; &nbsp; &nbsp; &nbsp;
If ColNum > 256 Then Exit Sub
&nbsp; &nbsp; &nbsp;
End If

&nbsp; &nbsp; &nbsp; Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _
&nbsp; &nbsp; &nbsp; &nbsp; = Application.WorksheetFunction.Transpose(Buffer())
&nbsp; &nbsp; &nbsp; RowNum = RowNum + BufferPtr
&nbsp; &nbsp;
End If

&nbsp; &nbsp; BufferPtr = 0
&nbsp; &nbsp;
If FlushBuffer = True Then
&nbsp; &nbsp; &nbsp; Erase Buffer
&nbsp; &nbsp; &nbsp; RowNum = 0
&nbsp; &nbsp; &nbsp; ColNum = 0
&nbsp; &nbsp; &nbsp;
Exit Sub
&nbsp; &nbsp;
Else
&nbsp; &nbsp; &nbsp;
ReDim Buffer(1 To UBound(Buffer))
&nbsp; &nbsp;
End If

&nbsp;
End If

&nbsp;
'construct the next set
&nbsp;
For i = 1 To UBound(ItemsChosen)
&nbsp; &nbsp; sValue = sValue & ', ' & vAllItems(ItemsChosen(i), 1)
&nbsp;
Next i

&nbsp;
'and save it in the buffer
&nbsp; BufferPtr = BufferPtr + 1
&nbsp; Buffer(BufferPtr) = Mid$(sValue, 3)
End Sub&nbsp; 'SavePermutation



@+

Gael

Message édité par: Gael, à: 01/09/2005 23:47
 

Discussions similaires

Réponses
13
Affichages
321

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 084
Messages
2 085 194
Membres
102 812
dernier inscrit
abdouami