Skip to main content
Logo image

Discrete Math with SageMath: Learn math with open-source software

Section 3.1 Combinatorics

Subsection 3.1.1 Factorial Function

The factorial of a non-negative integer \(n\text{,}\) denoted by \(n!\text{,}\) is the product of all positive integers less than or equal to \(n\text{.}\)
Compute the factorial of \(5\text{:}\)

Subsection 3.1.2 Combinations

The combination \((n, k)\) is an unordered selection of \(k\) objects from a set of \(n\) objects.
Calculate the number of ways to choose \(3\) elements from a set of \(5\text{:}\)
List the combinations:
Note that the function arranges the numbers from \(0\) to \(n - 1\) for its listing of combinations.
The binomial() function provides an alternative method to compute the number of combinations. While Combinations(mset, k), returns the combinatorial class of combinations of the multiset, binomial(x, m) returns the coefficient value or a symbolic expression.

Subsection 3.1.3 Permutations

A permutation \((n, k)\) is an ordered selection of \(k\) objects from a set of \(n\) objects.
To calculate the number of ways to choose \(3\) elements from a set of \(5\) when the order matters, use the Permutations() method.
List the permutations:
Note that the function arranges the numbers from \(1\) to \(n\) for its listing of permutations.
When \(n = k\text{,}\) we can calculate permutations of \(n\) elements.
Calculate the number of permutations of a set with \(3\) elements:
List the permutations:
The following is an example of permutations of specified elements:
Choose \(2\text{:}\)