How to use the PERMUT function
The PERMUT function returns the number of permutations for a set of elements that can be selected from a larger number of elements. The internal order of the elements is important in permutations.
Table of Contents
1. Introduction
What is a permutation?
A permutation is an arrangement of a set of items in a specific order where the order matters. In other words, permutations are the different ways in which a collection of distinct items can be arranged taking into account the order of the items.
For example, consider the set of letters {A, B, C}. The possible permutations of this set are:
ABC, ACB, BAC, BCA, CAB, and CBA
Each of these arrangements is a different permutation because the order of the items is different.
How is the number of permutations calculated?
The number of permutations of a set of n distinct items, taken r at a time, is given by the formula:
nPr = n! / (n - r)!
n! - (read as "n factorial") represents the product of all positive integers less than or equal to n
(n - r)! - represents the product of all positive integers less than or equal to (n - r)
For example, if we want to find the number of permutations of 4 items taken 2 at a time, we would calculate:
4P2 = 4! / (4 - 2)!
(5 * 4 * 3 * 2 * 1) / (2 * 1)
24 / 2
12
So, there are 12 possible permutations of 4 items taken 2 at a time.
What is the difference between permutations and combinations?
The main difference between permutations and combinations is whether the order of the items matters or not. Permutations are used when the order matters, and combinations are used when the order does not matter.
In permutations the order of the items matters. Each different arrangement of the items is considered a distinct permutation. For example, if we have the set {A, B, C}, the permutations ABC, ACB, BAC, BCA, CAB, and CBA are all distinct because the order of the items is different in each case.
In combinations, the order of the items does not matter. Two arrangements that differ only in the order of the items are considered the same combination. For example, if we have the set {A, B, C}, the combinations AB an BA are considered the same because they contain the same items, regardless of their order.
The permutations of 3 items taken 3 at a time (all items are used) are: ABC, ACB, BAC, BCA, CAB, CBA (6 permutations)
The combinations of 3 items taken 2 at a time (selecting 2 items from 3) are: AB, AC, BC (3 combinations)
Number of permutations of n items taken r at a time:
nPr = n! / (n - r)!
Number of combinations of n items taken r at a time:
nCr = n! / (r! * (n - r)!)
What is the difference between permutations with repetition and without repetition?
In permutations without repetition each item is distinct and no item can be repeated in the arrangement. For example, if we have the set {A, B, C}, the permutations without repetition are:
ABC, ACB, BAC, BCA, CAB, CBA
Here, each item appears exactly once in each permutation.
In permutations with repetition items can be repeated in the arrangement. For example, if we have the set {A, B}, the permutations with repetition are:
AA, AB, BA, and BB.
In this case, items are repeated in some of the permutations.
2. Syntax
PERMUT(number, number_chosen)
3. Arguments
number | Required. A whole number larger than 0 (zero) that represents the total number of elements. |
number_chosen | Required. A whole number larger than 0 (zero) that represents the number of elements in each permutation. |
4. Example
A restaurant has three spice options: chiles (A), black pepper (B), and vanilla (C). They want to create new flavors by combining two of these three spices together. The question is, how many different permutations or arrangements of two spices can be formed from the three available options? (The order matters)
To calculate the number of permutations, we can use the PERMUT function with the following arguments:
=PERMUT(n,r)
Where:
n = the total number of items (in this case, spices)
r = the number of items being selected or arranged
Formula in cell F3:
Column B, C and D demonstrate how many arrangements (permutations) there are when 2 elements are selected out of 3 elements [A, B, C]. Section 4 below demonstrates a formula that calculates all arrangements (permutations).
The six permutations are [A,B] ,[A,C] ,[B,C] , [B,A] , [C,B] and [C,A].
[A,B] - chiles and black pepper
[A,C] - chiles and vanilla
[B,C] - black pepper and vanilla
[B,A] - black pepper and chiles
[C,B] - vanilla and black pepper
[C,A] - vanilla and chiles
5. Function not working
The PERMUT function returns
- #VALUE! error if you use a non-numeric input value.
- #NUM! error if the number_chosen argument is larger than number argument.
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!), the function will return the same error.
5.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
5.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue.
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference C3 converted to hard-coded value using the F9 key. The PERMUT function requires numerical values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
5.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
6. Permutations without repetition - Excel 365 formula
The following formula deletes rows that contain duplicate values, this means you need the formula in cell I3 to create permutations with repetition before using the formula in cell L3.
Formula in cell L3:
Formula in cell I3:
The formula in cell I3 is demonstrated and explained here: Create a list of permutations - Excel 365
In other words, it creates permutations without repetition based on the output from the formula above that creates permutations with repetition.
7. Find the optimal permutation - Excel Solver
A machine has five different tools named A, B, C, D, and E. The table in cell range B2:G7 shows how much time is needed to change one tool to another tool. For example, changing tool A to tool B takes 5 minutes.
In what order do we need to change tools to find the least amount of time needed? The Solver lets you change the order in cell range I3:I7 automatically, the worksheet calculates the time in cells K3:K7 based on the table.
The Solver is a built-in feature that you can use in Excel to quickly find an optimal permutation.
Cell K3 contains a sum function:
The Solver uses the value in K3 to find the optimal solution by changing numbers in I3:I7. The formula in J3:J7 returns the Tool name.
Formula in cell J3:
The INDEX function gets the tool name in C2:G2 based on the number in cell I3. The number in cell I3 changes when the Solver tries different permutations.
Formula in cell K4:
Copy this formula to cells below as far as needed.
6.1 Explaining formula
Cell K3 contains 0 (zero). The formula in K4 uses the tool name in cell J4 and the tool name in the cell above J4 which is cell J3 to find the appropriate time value in the table.
Step 1 - Calculate row
The MATCH function returns the relative position of an item in an array or cell reference that matches a specified value in a specific order.
MATCH(lookup_value, lookup_array, [match_type])
MATCH(J4, $B$3:$B$7, 0)
becomes
MATCH("B", {"A";"B";"C";"D";"E"}, 0)
and returns 2.
Step 2 - Calculate column
MATCH(J3, $C$2:$G$2, 0)
becomes
MATCH("A", {"A","B","C","D","E"}, 0)
and returns 1.
Step 6 - Get time value
The INDEX function gets a value based on a row number and column number (optional).
INDEX(array, [row_num], [column_num], [area_num])
INDEX($C$3:$G$7, MATCH(J4, $B$3:$B$7, 0), MATCH(J3, $C$2:$G$2, 0))
becomes
INDEX($C$3:$G$7, 2, 1)
becomes
INDEX(
{"-", 5, 3, 7, 4;
5, "-", 9, 11, 8;
3, 9, "-", 2, 5;
7, 11, 2, "-", 7;
4, 8, 5, 7, "-"}
, 2, 1)
and returns 5. 5 is the first value in the second row.
7.2 Setting up the Solver
- Go to tab "Data".
- Press with the left mouse button on the "Solver" button.
- Press with left mouse button on the arrow next to "Set Objective" and select cell K9. This value is the sum of all times K3:K7.
- Press with the left mouse button on the radio button named "Min" to select it. This lets the solver know that we are looking for a permutation that returns the smallest number.
- Press with left mouse button on the arrow next to "By changing variable cells" and select cells I3:I7. These cells are populated with different permutations.
- Press with the left mouse button on the "Add" button. A dialog box appears, this lets you apply constraints.
- Press with mouse on the arrow next to "Cell Reference:", then select cell range I3:I7.
- Press with mouse on the equal sign, select "dif", this lets the Solver know that all numbers in cell range I3:I7 must be different.
- Press with left mouse button on the "OK" button to return to the previous dialog box.
- Change solving method to "Evolutionary".
- Press with mouse on the check box "Make unconstrained variables Non-negative to enable it.
- Press with left mouse button on the "Solve" button to start.
- A dialog box appears, press with left mouse button on the "OK" button.
The solver found permutations iteration 4, 3, 5, 1, and 2 to be the optimal solution. In other words, this permutation returns the lowest wait time for a given set of tools.
8. List permutations with repetition - UDF
This blog post demonstrates a custom function (UDF) that creates permutations. Repetition is allowed. The custom function lets you specify the number of items to use and it will return an array of numbers.
Array formula:
To enter an array formula, type the formula in a cell then press and hold CTRL + SHIFT simultaneously, now press Enter once. Release all keys.
The formula bar now shows the formula with a beginning and ending curly bracket telling you that you entered the formula successfully. Don't enter the curly brackets yourself.
8.1 VBA Code
Function ListPermut(num As Integer) ' Permutations with repetition ' Declare variables to store intermediate calculations ' c is a counter variable for columns (elements in the permutation) ' r is a counter variable for rows (each permutation) ' p stores the total number of permutations Dim c As Long, r As Long, p As Long ' Declare a 2D array 'rng' to store the permutations Dim rng() As Long ' Calculate the total number of permutations using the formula: num^num p = num ^ num ' Resize the 'rng' array to have 'p' rows and 'num' columns ReDim rng(1 To p, 1 To num) ' Initialize the first row of the 'rng' array with consecutive numbers from 1 to 'num' For c = 1 To num rng(1, c) = 1 Next c ' Generate the remaining permutations row by row For r = 2 To p For c = num To 1 Step -1 ' If it's the last column, increment the value from the previous row If c = num Then rng(r, c) = rng(r - 1, c) + 1 ' If the current value is 0, copy the value from the previous row ElseIf rng(r, c) = 0 Then rng(r, c) = rng(r - 1, c) End If ' If the current value exceeds 'num', reset it to 1 and increment the value in the previous column If rng(r, c) = num + 1 Then rng(r, c) = 1 rng(r, c - 1) = rng(r - 1, c - 1) + 1 End If Next c Next r ' Return the 'rng' array containing all permutations ListPermut = rng End Function
7.2 How to add the user defined function to your workbook
The image above shows a macro that is not used in this article, it is only there to show you where to paste the code.
- Press Alt-F11 to open visual basic editor.
- Press with left mouse button on Module on the Insert menu.
- Copy the above user defined function.
- Paste it to the code module.
- Exit visual basic editor.
- Select sheet1.
- Select cell range A1:C27.
- Type =ListPermut(3) in formula bar and press CTRL+SHIFT+ENTER (Array formula).
If you don't know how to enter an array formula then read the detailed instructions below the image.
8.3 Example
Array formula in A3:C29:
'PERMUT' function examples
I discussed the difference between permutations and combinations in my last post, today I want to talk about two kinds […]
Functions in 'Statistical' category
The PERMUT function function is one of 73 functions in the 'Statistical' category.
Excel function categories
Excel categories
5 Responses to “How to use the PERMUT function”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
I would need to use the ListPermut function with argument 7. As I can understand it only works to a maximum of 6.
Thanks,
Antonio
I'll post an answer as soon as I can.
I'm trying to use the function for obtaining the possibilities of distributing 3 things over 5 places (repetition allowed), but the function is not workings on this option, can you please help ??
example:
A,A,B,B,C
A,A,B,B,B
A,C,B,B,A
and so on....
Thanks in advance
and also the number 5 can be changed to be from 1 to 8 but the number 3 is constant. Thank you
I have found the equation in section 4 very helpful for what I am using it for, thank you for this!
I notice that the equation only limits you to a chosen number of 4, if I wanted for example 5, then this would break the formula, but I found that is because there are more rows in the array than there are in the whole excel worksheet. For example, for PERMUTATIONA(21,5) there are 4,084,101 permutations and excel only has 1,048,576 rows.
However, I do not want all 4,084,101 permutations, but rather only the ones that sum up to a given value.
For example, I have the variables {0, 5, 10, 15, 20, 25,..., 100} that I want to find all the possible permutations with repetition where any 5 numbers sum up to 35. This should dramatically reduce the number of rows in the array to 330 possible permutations.
Is there a way to include this in the already built formula? I feel like there is but I'm struggling to figure it out.