Python math.perm() Method
The Python math.perm() method is used to calculate the number of permutations of "k" items chosen from a set of "n" distinct items in a specific order. Mathematically, it is denoted as β
P(n,\:k)\:=\:\frac{n!}{(n\:-\:k)!}
Where, n! represents the factorial of "n", which is the product of all positive integers from 1 to n, and (nk)! represents the factorial of (n-k).
For example, if you have a set of "5" distinct items and you want to arrange "3" of them, "math.perm(5, 3)" will return the number of permutations, which is 5!/(5-3)! = 5!/2! = 5 Γ 4 Γ 3 Γ 2 Γ 1/2 Γ 1 =60.
Syntax
Following is the basic syntax of the Python math.perm() method β
math.perm(n, k)
Parameters
This method accepts the following parameters β
x β It is the total number of items or elements.
y β It is the number of items to be arranged (permutations).
Return Value
The method returns the permutation of the given values "n" and "k".
Example 1
In the following example, we are calculating the number of permutations of selecting "3" elements from a set of "5" distinct elements β
import math
result = math.perm(5, 3)
print("The result obtained is:",result)
Output
The output obtained is as follows β
The result obtained is: 60
Example 2
Here, we are calculating the number of permutations of selecting "2" elements from a set of "4" elements with replacement allowed β
import math
result = math.perm(4, 2)
print("The result obtained is:",result)
Output
Following is the output of the above code β
The result obtained is: 12
Example 3
Now, we are calculating the number of permutations of arranging the letters of the word "MISSISSIPPI" while considering the duplicate letters β
import math
word = "MISSISSIPPI"
n = len(word)
result = math.perm(n)
print("The result is:",result)
Output
We get the output as shown below β
The result is: 39916800
Example 4
In this example, we are calculating the number of permutations of selecting "0" elements from an empty set β
import math
result = math.perm(0, 0)
print("The result obtained is:",result)
Output
The result produced is as shown below β
The result obtained is: 1