Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Python Code Example: Permutation and Combination of 2 Numbers

This Python code example calculates the combination and permutation of two user-provided integers, n and r. It defines a recursive function, fact, to compute the factorial of a given number.

Code Example

def fact(val):
    # Base case: If val is 0 or 1, return 1 as the factorial of 0 and 1 is 1
    if val == 0 or val == 1:
        return 1
    else:
        # Recursive case: Return val multiplied by the factorial of val-1
        return val * fact(val - 1)

# Prompt the user to enter the value of n and convert it to an integer
n = int(input("Enter value of n : "))

# Prompt the user to enter the value of r and convert it to an integer
r = int(input("Enter value of r : "))

# Calculate the combination using the formula nCr = n! / (r!(n-r)!)
combination = int(fact(n) / (fact(r) * fact(n - r)))

# Calculate the permutation using the formula nPr = n! / (n-r)!
permutation = int(fact(n) / fact(n - r))

# Print the calculated combination value
print("\nCombination : " + str(combination))

# Print the calculated permutation value
print("Permutation : " + str(permutation))

Output

Enter value of n : 4
Enter value of r : 3

Combination : 4
Permutation : 24

Code Explanation

Function to Calculate Factorial

def fact(val):
    if val == 0 or val == 1:
        return 1
    else:
        return val * fact(val - 1)
  1. Function Definition: def fact(val): defines a function named fact that takes a single argument val.
  2. Base Case: if val == 0 or val == 1: return 1 checks if val is 0 or 1. If true, it returns 1 because the factorial of 0 or 1 is 1.
  3. Recursive Case: else: return val * fact(val - 1) computes the factorial of val by multiplying val by the factorial of val - 1. This is a recursive call where the function calls itself with a decremented value of val.

Input from the User

n = int(input("Enter value of n : "))
r = int(input("Enter value of r : "))
  1. Input for n: n = int(input("Enter value of n : ")) prompts the user to enter a value for n, converts the input to an integer, and assigns it to the variable n.
  2. Input for r: r = int(input("Enter value of r : ")) prompts the user to enter a value for r, converts the input to an integer, and assigns it to the variable r.

Calculating Combination and Permutation

combination = int(fact(n) / (fact(r) * fact(n - r)))
permutation = int(fact(n) / fact(n - r))
  1. Combination Calculation:
    • fact(n): Computes the factorial of n.
    • fact(r): Computes the factorial of r.
    • fact(n - r): Computes the factorial of n - r.
    • fact(n) / (fact(r) * fact(n - r)): Computes the combination formula (π‘›π‘Ÿ)=𝑛!π‘Ÿ!(π‘›βˆ’π‘Ÿ)!(rn​)=r!(nβˆ’r)!n!​.
    • int(...): Converts the result to an integer and assigns it to the variable combination.
  2. Permutation Calculation:
    • fact(n): Computes the factorial of n.
    • fact(n - r): Computes the factorial of n - r.
    • fact(n) / fact(n - r): Computes the permutation formula 𝑃(𝑛,π‘Ÿ)=𝑛!(π‘›βˆ’π‘Ÿ)!P(n,r)=(nβˆ’r)!n!​.
    • int(...): Converts the result to an integer and assigns it to the variable permutation.

Printing the Results

print("\nCombination : " + str(combination))
print("Permutation : " + str(permutation))
  1. Printing Combination: print("\nCombination : " + str(combination)) prints the string “Combination :” followed by the value of combination.
  2. Printing Permutation: print("Permutation : " + str(permutation)) prints the string “Permutation :” followed by the value of permutation.