Permutation and combination are the mathematical principles that help in counting the number of possible outcomes of an event. These principles are widely used in various fields like statistics, mathematics, computer science, etc. In this article, we will discuss permutation and combination in Python, their formulas, and how to implement them in Python programming language.
Table of Contents
TogglePermutation:
Permutation is a way of selecting and arranging objects from a set of objects where the order of selection matters. It is expressed as nP r, where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.
Formula for permutation:
nP r = n! / (n-r)!
where,
n! represents factorial of n, which is the product of all positive integers from 1 to n, and 0! = 1.
Example of permutation:
Suppose we have five balls numbered 1,2,3,4,5. How many different ways can we select and arrange two balls from them?
Here, n = 5 and r = 2.
To calculate the number of permutations, we can use the formula,
nP r = n! / (n-r)!
nP r = 5! / (5-2)!
nP r = 5! / 3!
nP r = (5*4*3*2*1) / (3*2*1)
nP r = 20
Therefore, there are 20 different ways of selecting and arranging two balls from five balls.
Python program for permutation:
We can implement the formula for permutation using Python programming language. Here is the Python code for permutation.
import math
n = int(input("Enter total number of objects n: "))
r = int(input("Enter number of objects selected r: "))
# Formula for permutation
permutation = math.factorial(n) / math.factorial(n-r)
print("Number of permutations: ", permutation)
In this code, we have imported the math module, which provides access to the mathematical functions. We have taken inputs for n and r from the user, and then we have applied the formula for permutation formula to calculate the number of permutations.
Combination:
Combination is a way of selecting objects from a set of objects where the order of selection does not matter. It is expressed as nC r, where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.
Formula for combination:
nCr = n! / (r! * (n-r)!)
Example of combination:
Suppose we have five balls numbered 1,2,3,4,5. How many different ways can we select two balls from them?
Here, n = 5 and r = 2.
To calculate the number of combinations, we can use the formula,
nCr = n! / (r! * (n-r)!)
nCr = 5! / (2! * (5-2)!)
nCr = 5! / (2! * 3!)
nCr = (5*4*3*2*1) / (2*1 * 3*2*1)
nCr = 10
Therefore, there are 10 different ways of selecting two balls from five balls.
Python program for combination:
We can implement the formula for combination using Python programming language. Here is the Python code for combination.
import math
n = int(input("Enter total number of objects n: "))
r = int(input("Enter number of objects selected r: "))
# Formula for combination
combination = math.factorial(n) / (math.factorial(r) * math.factorial(n-r))
print("Number of combinations: ", combination)
In this code, we have imported the math module, which provides access to the mathematical functions. We have taken inputs for n and r from the user, and then we have applied the formula for combination formula to calculate the number of combinations.
FAQs:
Q1. What is the difference between permutation and combination?
Ans. Permutation is a way of selecting and arranging objects from a set of objects where the order of selection matters. Whereas, combination is a way of selecting objects from a set of objects where the order of selection does not matter.
Q2. What is the formula for permutation?
Ans. The formula for permutation is nP r = n! / (n-r)!, where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.
Q3. What is the formula for combination?
Ans. The formula for combination is nCr = n! / (r! * (n-r)!), where n represents the total number of objects, and r denotes the number of objects that are selected from n objects.
Q4. How do we implement permutation and combination in Python?
Ans. We can implement permutation and combination using Python programming language by applying the respective formulas using math module in Python.
Conclusion:
In this article, we have discussed permutation and combination in Python, their formulas, and how to implement them in Python programming language. These principles are important in various fields like statistics, mathematics, and computer science. The formulas for permutation and combination can be easily implemented in Python programming language to calculate the number of possible outcomes of an event.