The perm function of math module in Python

Method Name:

perm()

Method Signature:

perm(n, k=None)

Parameters:

n – Number of items

k – Number of items to be chosen.

Return value:

Returns the number of ways to select ‘k’ items from number of items ‘n’, with order and without repetition.

Overview:

  • Like the comb() function perm() is a combinatoric function.
  • The perm() function of Python math module returns the number of ways to select ‘k’ items from number of items ‘n’, with order and without repetition.
  • The computation is given by: n! / (n - k)!, where k <= n; returns zero when 'k' is greater than 'n'.
  • When ‘k’ is None or when ‘k’ is equal to ‘n’, the function returns n! .

Example:

# Example python program to compute permutations -  Number of ways to choose k items from n # items

import math

 

totalItems      = 52; # n

itemsToChoose   = 5;  # k

 

# Number of ways to choose k items from n items (without repetition and with order)

permutations    = math.perm(totalItems, itemsToChoose);

 

print("Number of ways to choose %d items(k) from %d items(n)(without repetition & with order):"%(totalItems, itemsToChoose));

print(permutations);

 

Output:

Number of ways to choose 52 items(k) from 5 items(n)(without repetition & with order):

311875200


Copyright 2024 © pythontic.com