Permutation in Python

 Basics of the post

1. The permutation means changing a linear order set into a new order set. Simply said, it means rearranging the elements of a set in an organized way. 

2. The basic formula of permutation is given below. 'n' represents the total number of elements and 'r' represent the number of selected elements in a set.

npr

Read Similar Post: Combination in Python

Step to solve permutation

1. First of all, we will take two input values for 'n' and 'r'

n=int(input())
r=int(input
())

2. we will find the factorial of a number using the recursion process. This works will be done in a function name factorial.

def factorial(n):
if (n>=1):
return n*factorial(n-1)
else:
return 1

3. Then, the formula of permutation will be implemented and it is blocked in a function name permutation.

def permutaion(n, r):
return factorial(n) / factorial(n-r
)

4. After that, we will call the permutation function which receives 'n' and 'r' parameters. 

result = int(permutaion(n,r))
print(result)

Now the final code will be like below, 

Permutation in Python

n=int(input())
r=int(input())

def factorial(n):
if (n>=1):
return n*factorial(n-1)
else:
return 1

def permutaion(n, r):
return factorial(n) / factorial(n-r)

result = int(permutaion(n,r))
print(result)

Sample Input:

100   /* n=100 */

10  /* r=10 */

Sample Output:

62815650955529469952

Post a Comment

Previous Post Next Post