Calculate NCR and NPR in Python



In combinatorics, nCr and nPr are essential formulas used to calculate combinations and permutations. They represent the number of ways to select and arrange objects from a set. nCr refers to combinations, where order does not matter, while nPr refers to permutations, where order does matter.

The permutation refers to the arrangement of objects in a specific order.A combination refers to the selection of objects without taking order in reference. In this article, we are going to learn how we can calculate nCr and nPr in Python using different approaches.

The formula for nCr and nPr

The formulas for nCr and nPr are as follows:

Combination (nCr): n! / r!?(n?r)!Permutation (nPr): n! / (n?r)!

Where, n: Total number of objects, r: Number of objects to select or arrange, !: Factorial of a number. Let's look at the different approaches for calculating nCr and nPr in Python.

Using the Direct Formula Approach

In this approach, we use the direct formula to calculate nCr and nPr. In Python, we can manually implement the factorial calculation formula. The formula for calculating combination and permutation are:

For combination nCr: n! / r!?(n?r)!
For permuation nPr: n! / (n?r)!

Steps for Implementation

  • Define a function to calculate the factorial of a number.
  • Inside the function write the logic to calculate permutation and combination.
  • Use the factorial function to calculate nCr and nPr using their respective formulas.
  • Output the result.
Code Implementation
def factorial(n):
    if n == 0 or n == 1:
        return 1
    return n * factorial(n - 1)
def calculate_nCr(n, r):
    return factorial(n) // (factorial(r) * factorial(n - r))
def calculate_nPr(n, r):
    return factorial(n) // factorial(n - r)
n = 6
r = 2
nCr = calculate_nCr(n, r)
nPr = calculate_nPr(n, r)
print(f"nCr for n={n} and r={r} is: {nCr}")
print(f"nPr for n={n} and r={r} is: {nPr}")
Output
nCr for n=6 and r=2 is: 15
nPr for n=6 and r=2 is: 30

Using Python's math Module

In this approach, we use Python's built-in math module to efficiently calculate the factorial of the number. It optimized the function for factorial calculation. We can use this module to calculate nCr and nPr.

Steps for Implementation

  • Import the factorial function from the math module.
  • Use the nCr and nPr formulas directly.
  • Output the result.
Code Implementation
from math import factorial
def calculate_nCr(n, r):
    return factorial(n) // (factorial(r) * factorial(n - r))
def calculate_nPr(n, r):
    return factorial(n) // factorial(n - r)
n = 7
r = 3
nCr = calculate_nCr(n, r)
nPr = calculate_nPr(n, r)
print(f"nCr for n={n} and r={r} is: {nCr}")
print(f"nPr for n={n} and r={r} is: {nPr}")
Output
nCr for n=7 and r=3 is: 35
nPr for n=7 and r=3 is: 210

Using math.comb and math.perm Functions

In this approach, we use Python functions comb and perm directly to find permutation and combination directly. In Python 3.8 and later the Python version, the math module introduced the comb and perm functions.

Steps for Implementation

  • Import the comb and perm functions from the math module.
  • Use these functions to directly calculate nCr and nPr.
  • Output the result.
Code Implementation
from math import comb, perm
n = 10
r = 4
nCr = comb(n, r)
nPr = perm(n, r)
print(f"nCr for n={n} and r={r} is: {nCr}")
print(f"nPr for n={n} and r={r} is: {nPr}")
Output
nCr for n=10 and r=4 is: 210
nPr for n=10 and r=4 is: 5040

Using Recursive Approach

In this approach, we use a recursive method to calculate permutation and combination. In this approach, we use recursion to compute the factorial values for the formulas. This is an alternate method, but it is less efficient than the direct approach.

Steps for Implementation

  • Define a recursive function for calculating the factorial.
  • Use the recursive factorial function to compute permutation and combination.
  • For nCr use nCr=(n?1)C(r?1)+(n?1)C(r) and for nPr recursion can be used to compute the result directly by decrementing n and r.
  • Output both results.
Code Implementation
def recursive_factorial(n):
    if n == 0 or n == 1:
        return 1
    return n * recursive_factorial(n - 1)
def calculate_nCr_nPr_recursive(n, r):
    nCr = recursive_factorial(n) // (recursive_factorial(r) * recursive_factorial(n - r))
    nPr = recursive_factorial(n) // recursive_factorial(n - r)
    return nCr, nPr
n = 4
r = 2
nCr, nPr = calculate_nCr_nPr_recursive(n, r)
print(f"The value of nCr (C({n},{r})) is: {nCr}")
print(f"The value of nPr (P({n},{r})) is: {nPr}")
Output
The value of nCr (C(4,2)) is: 6
The value of nPr (P(4,2)) is: 12
Updated on: 2025-02-05T13:03:14+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements