Combinations & Permutations with Replacement (Repetition)

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
As I understand it, the combin & permut functions only calculate the results without replacement. There are no corresponding functions for with replacement. Is that correct?

The formula for permutations with replacement is just n^k, but the one for combinations is ugly: (n+k-1)!/k!(n-1)!.

Do I need to write a UDF to avoid having to enter that each time and probably make a typo 30% of the time?

Would it have killed the M$FT developers to have added a third parameter to these functions, something like 0/1 where 0 = without replacement and 1 = with? They could have set the default to without replacement so that there would be no compatibility problems. Seems obvious and simple.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
As far as I'm aware you'll need to write your own UDF, this is one suggestion:
Code:
Function Combinations_Replacement(ByRef k As Long, ByRef n As Long) As Double
 
    Dim x               As Long
    Dim numer           As Long
    Dim denom(1 To 2)   As Long
    
    '(n-1+k)!/k!(n-1)!
    n = n - 1
    
    For x = 1 To n + k
        numer = numer + x
        If x <= k Then denom(1) = denom(1) + x
        If x <= n Then denom(2) = denom(2) + x
    Next x
    
    Combinations_Replacement = numer / (denom(1) * denom(2))
    
    Erase denom
    
End Function
 
Upvote 0
Sorry, misread factorial, try:
Code:
Function Combinations_Replacement(ByRef k As Long, ByRef n As Long) As Double
 
    Dim x               As Long
    Dim numer           As Long
    Dim denom(1 To 2)   As Long
    
    '(n-1+k)!/k!(n-1)!
    n = n - 1
    numer = 1: demon(1) = 1: denom(2) = 1
    
    For x = 1 To n + k
        numer = numer * x
        If x <= k Then denom(1) = denom(1) * x
        If x <= n Then denom(2) = denom(2) * x
    Next x
    
    Combinations_Replacement = numer / (denom(1) * denom(2))
    
    Erase denom
    
End Function
 
Upvote 0
You're welcome, last correction! (typo):
Rich (BB code):
Function Combinations_Replacement(ByRef k As Long, ByRef n As Long) As Double
 
    Dim x               As Long
    Dim numer           As Long
    Dim denom(1 To 2)   As Long
    
    '(n-1+k)!/k!(n-1)!
    n = n - 1
    numer = 1: denom(1) = 1: denom(2) = 1
    
    For x = 1 To n + k
        numer = numer * x
        If x <= k Then denom(1) = denom(1) * x
        If x <= n Then denom(2) = denom(2) * x
    Next x
    
    Combinations_Replacement = numer / (denom(1) * denom(2))
    
    Erase denom
    
End Function
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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