Skip to main content

Stirling Number Calculator

Calculate Stirling numbers of the first and second kind for combinatorial problems. Enter values for instant results with step-by-step formulas.

Skip to calculator
Mathematics

Stirling Number Calculator

Calculate Stirling numbers of the first and second kind for combinatorial problems. Find set partitions, permutation cycles, and Bell numbers.

Last updated: December 2025Reviewed by NovaCalculator Mathematics Team

Calculator

Adjust values & calculate
5
3
S(5, 3)
25
Ways to partition 5 elements into 3 non-empty subsets
Bell Number B(5)
52
5!
120

S(n, k) Triangle

n=0:1
n=1:01
n=2:011
n=3:0131
n=4:01761
n=5:011525101
Note: Stirling numbers grow very rapidly. This calculator supports values up to n = 20 for accurate integer results.
Your Result
S(5, 3) = 25 | Bell(5) = 52 | 5! = 120
Share Your Result
Understand the Math

Formula

S(n, k) = k * S(n-1, k) + S(n-1, k-1)

Where S(n, k) is the Stirling number of the second kind counting the number of partitions of an n-element set into k non-empty subsets. For the first kind, |s(n, k)| = (n-1) * |s(n-1, k)| + |s(n-1, k-1)| counts permutations with k cycles.

Last reviewed: December 2025

Worked Examples

Example 1: Partitioning Students into Study Groups

In how many ways can 5 students be divided into exactly 3 non-empty study groups?
Solution:
We need S(5, 3), the Stirling number of the second kind. Using the recurrence: S(5, 3) = 3 * S(4, 3) + S(4, 2) S(4, 3) = 6, S(4, 2) = 7 S(5, 3) = 3 * 6 + 7 = 18 + 7 = 25 So there are 25 ways to partition 5 students into 3 non-empty groups.
Result: S(5, 3) = 25 ways to partition 5 elements into 3 subsets

Example 2: Counting Permutations by Cycle Structure

How many permutations of 4 elements have exactly 2 cycles?
Solution:
We need |s(4, 2)|, the unsigned Stirling number of the first kind. Using the recurrence: |s(4, 2)| = 3 * |s(3, 2)| + |s(3, 1)| |s(3, 2)| = 3, |s(3, 1)| = 2 |s(4, 2)| = 3 * 3 + 2 = 9 + 2 = 11 So there are 11 permutations of {1, 2, 3, 4} with exactly 2 disjoint cycles.
Result: |s(4, 2)| = 11 permutations with exactly 2 cycles
Expert Insights

Background & Theory

The Stirling Number Calculator applies the following established principles and formulas. Mathematics rests on a hierarchy of number systems, each extending the previous. The natural numbers (1, 2, 3, ...) support counting and ordering. The integers add negative values and zero, enabling subtraction without restriction. The rational numbers, expressible as p/q where p and q are integers and q is nonzero, close the system under division. The real numbers fill the gaps left by irrationals such as the square root of 2 or pi, forming a complete ordered field. The complex numbers, written as a + bi where i is the square root of negative one, complete the algebraic closure of the reals and allow every polynomial to have a root. Prime factorization states that every integer greater than one is uniquely expressible as a product of primes, a result known as the Fundamental Theorem of Arithmetic. Computing the greatest common divisor (GCD) of two integers relies most efficiently on the Euclidean algorithm: repeatedly replace the larger number with the remainder when it is divided by the smaller, until the remainder is zero. The last nonzero remainder is the GCD. The least common multiple (LCM) follows from the identity LCM(a, b) = |a * b| / GCD(a, b). Modular arithmetic defines equivalence classes of integers that share the same remainder under division by a modulus n. Fermat's Little Theorem and Euler's Theorem arise from this structure and underpin modern cryptography. Logarithms are the inverses of exponential functions. If b raised to the power x equals y, then the logarithm base b of y equals x. The natural logarithm uses base e, approximately 2.71828. Combinatorics counts arrangements and selections. The number of ordered arrangements (permutations) of r objects from n distinct objects is nPr = n! / (n - r)!. The number of unordered selections (combinations) is nCr = n! / (r! * (n - r)!). Pascal's triangle arranges these binomial coefficients so that each entry equals the sum of the two entries directly above it. The Fibonacci sequence, defined by F(1) = 1, F(2) = 1, and F(n) = F(n-1) + F(n-2), appears throughout nature and connects deeply to the golden ratio via Binet's formula.

History

The history behind the Stirling Number Calculator traces back through the following developments. Mathematics as a systematic discipline traces to ancient Mesopotamia. Babylonian clay tablets dating to around 1800 BCE demonstrate knowledge of quadratic equations, Pythagorean triples, and base-60 arithmetic, suggesting a practical mathematical tradition far preceding Greek formalism. Euclid of Alexandria compiled the Elements around 300 BCE, establishing the axiomatic method that would define rigorous mathematics for over two thousand years. His work organized plane geometry, number theory, and proportion into logically chained propositions derived from a small set of postulates. The algorithm bearing his name for computing GCDs appears in Book VII and remains in use today. In the 9th century, the Persian scholar Muhammad ibn Musa Al-Khwarizmi wrote Al-Kitab al-mukhtasar fi hisab al-jabr wal-muqabala, the treatise whose title gave algebra its name. He systematized the solution of linear and quadratic equations and described procedures that operated on unknowns as objects, a conceptual leap away from purely numerical calculation. Rene Descartes introduced coordinate geometry in 1637 by uniting algebra and Euclidean geometry, allowing curves to be studied through equations. This synthesis set the stage for calculus. Isaac Newton and Gottfried Wilhelm Leibniz independently developed calculus during the 1660s and 1670s, triggering a priority dispute that lasted decades and divided British and Continental mathematicians. Carl Friedrich Gauss proved the Fundamental Theorem of Algebra in 1799, showing that every nonconstant polynomial has at least one complex root. His Disquisitiones Arithmeticae of 1801 established modern number theory. David Hilbert's formalist program at the turn of the 20th century sought to place all of mathematics on an explicit axiomatic foundation, a project that Kurt Godel's incompleteness theorems of 1931 showed to be fundamentally limited. Alan Turing's work in the 1930s on computability introduced the theoretical model of the stored-program computer and linked mathematical logic directly to the limits of algorithmic calculation. His proof that no algorithm can decide in general whether an arbitrary program will halt or run forever placed fundamental boundaries on what mathematics can mechanically determine, and it opened the discipline now known as theoretical computer science.

Share this calculator

Explore More

Frequently Asked Questions

Stirling numbers are fundamental combinatorial quantities that appear in many areas of mathematics including combinatorics, number theory, and analysis. There are two kinds of Stirling numbers. Stirling numbers of the second kind S(n, k) count the number of ways to partition a set of n elements into exactly k non-empty subsets. Stirling numbers of the first kind s(n, k) (unsigned) count the number of permutations of n elements with exactly k disjoint cycles. Both kinds satisfy useful recurrence relations and appear in formulas for converting between ordinary and factorial powers of polynomials.
Stirling numbers of the second kind, denoted S(n, k), count set partitions. For example, S(4, 2) = 7 means there are 7 ways to split the set {1, 2, 3, 4} into exactly 2 non-empty groups. Stirling numbers of the first kind, denoted |s(n, k)| when unsigned, count permutations by cycle structure. For instance, |s(4, 2)| = 11 means there are 11 permutations of 4 elements that decompose into exactly 2 cycles. While both are triangular arrays like Pascal triangle, they serve different combinatorial purposes and satisfy different recurrences: S(n, k) = k * S(n-1, k) + S(n-1, k-1), while |s(n, k)| = (n-1) * |s(n-1, k)| + |s(n-1, k-1)|.
Stirling numbers of the second kind can be calculated using the recurrence relation S(n, k) = k * S(n-1, k) + S(n-1, k-1), with base cases S(0, 0) = 1 and S(n, 0) = S(0, k) = 0 for n, k greater than 0. The intuition behind the recurrence is that element n either joins an existing partition (k choices, giving k * S(n-1, k)) or forms its own singleton partition (giving S(n-1, k-1)). There is also an explicit formula using inclusion-exclusion: S(n, k) = (1/k!) times the sum from j = 0 to k of (-1)^j times C(k, j) times (k - j)^n. Dynamic programming with a table is the most efficient approach for computing multiple values.
Bell numbers are the row sums of the Stirling numbers of the second kind. The nth Bell number B(n) equals the sum of S(n, k) for k from 0 to n, representing the total number of ways to partition a set of n elements into any number of non-empty subsets. For example, B(4) = S(4,1) + S(4,2) + S(4,3) + S(4,4) = 1 + 7 + 6 + 1 = 15. Bell numbers grow very rapidly: B(10) = 115,975 and B(15) = 1,382,958,545. They can also be computed using the Bell triangle, a construction similar to Pascal triangle, and have applications in set theory, combinatorics, and computer science.
Stirling numbers appear in numerous practical applications across mathematics and computer science. In statistics, they are used for computing moments of distributions and in the theory of cumulants. In computer science, Stirling numbers of the second kind model the number of ways to distribute n distinct objects into k identical containers (like hash table analysis). They appear in the analysis of algorithms, particularly sorting and searching algorithms. In numerical analysis, they connect forward differences to derivatives. In combinatorial chemistry, they help count molecular configurations. They also appear in coding theory and in the enumeration of surjective functions between finite sets.
The unsigned Stirling numbers of the first kind satisfy the recurrence |s(n, k)| = (n - 1) * |s(n-1, k)| + |s(n-1, k-1)|, with base cases |s(0, 0)| = 1 and |s(n, 0)| = |s(0, k)| = 0 for positive n and k. The combinatorial interpretation is clear: when inserting element n into a permutation of {1, ..., n-1}, it either gets inserted into one of the existing cycles (n - 1 positions, one before each existing element) keeping the cycle count the same, or it forms a new fixed-point cycle by itself, increasing the cycle count by one. Signed Stirling numbers of the first kind include an alternating sign factor: s(n, k) = (-1)^(n-k) * |s(n, k)|.
Educational Note: This calculator is provided for educational and informational purposes. Results are based on the formulas and inputs provided. Always verify important calculations independently. NovaCalculator processes calculator inputs client-side; optional analytics follow visitor consent settings.Reviewed by: NovaCalculator Mathematics Team โ€” Verified against standard mathematical and scientific references. Last reviewed: December 2025. ยฉ 2024โ€“2026 NovaCalculator.

Share this calculator

Formula

S(n, k) = k * S(n-1, k) + S(n-1, k-1)

Where S(n, k) is the Stirling number of the second kind counting the number of partitions of an n-element set into k non-empty subsets. For the first kind, |s(n, k)| = (n-1) * |s(n-1, k)| + |s(n-1, k-1)| counts permutations with k cycles.

Worked Examples

Example 1: Partitioning Students into Study Groups

Problem: In how many ways can 5 students be divided into exactly 3 non-empty study groups?

Solution: We need S(5, 3), the Stirling number of the second kind.\nUsing the recurrence: S(5, 3) = 3 * S(4, 3) + S(4, 2)\nS(4, 3) = 6, S(4, 2) = 7\nS(5, 3) = 3 * 6 + 7 = 18 + 7 = 25\nSo there are 25 ways to partition 5 students into 3 non-empty groups.

Result: S(5, 3) = 25 ways to partition 5 elements into 3 subsets

Example 2: Counting Permutations by Cycle Structure

Problem: How many permutations of 4 elements have exactly 2 cycles?

Solution: We need |s(4, 2)|, the unsigned Stirling number of the first kind.\nUsing the recurrence: |s(4, 2)| = 3 * |s(3, 2)| + |s(3, 1)|\n|s(3, 2)| = 3, |s(3, 1)| = 2\n|s(4, 2)| = 3 * 3 + 2 = 9 + 2 = 11\nSo there are 11 permutations of {1, 2, 3, 4} with exactly 2 disjoint cycles.

Result: |s(4, 2)| = 11 permutations with exactly 2 cycles

Frequently Asked Questions

What are Stirling numbers and why are they important?

Stirling numbers are fundamental combinatorial quantities that appear in many areas of mathematics including combinatorics, number theory, and analysis. There are two kinds of Stirling numbers. Stirling numbers of the second kind S(n, k) count the number of ways to partition a set of n elements into exactly k non-empty subsets. Stirling numbers of the first kind s(n, k) (unsigned) count the number of permutations of n elements with exactly k disjoint cycles. Both kinds satisfy useful recurrence relations and appear in formulas for converting between ordinary and factorial powers of polynomials.

What is the difference between Stirling numbers of the first and second kind?

Stirling numbers of the second kind, denoted S(n, k), count set partitions. For example, S(4, 2) = 7 means there are 7 ways to split the set {1, 2, 3, 4} into exactly 2 non-empty groups. Stirling numbers of the first kind, denoted |s(n, k)| when unsigned, count permutations by cycle structure. For instance, |s(4, 2)| = 11 means there are 11 permutations of 4 elements that decompose into exactly 2 cycles. While both are triangular arrays like Pascal triangle, they serve different combinatorial purposes and satisfy different recurrences: S(n, k) = k * S(n-1, k) + S(n-1, k-1), while |s(n, k)| = (n-1) * |s(n-1, k)| + |s(n-1, k-1)|.

How do you calculate Stirling numbers of the second kind?

Stirling numbers of the second kind can be calculated using the recurrence relation S(n, k) = k * S(n-1, k) + S(n-1, k-1), with base cases S(0, 0) = 1 and S(n, 0) = S(0, k) = 0 for n, k greater than 0. The intuition behind the recurrence is that element n either joins an existing partition (k choices, giving k * S(n-1, k)) or forms its own singleton partition (giving S(n-1, k-1)). There is also an explicit formula using inclusion-exclusion: S(n, k) = (1/k!) times the sum from j = 0 to k of (-1)^j times C(k, j) times (k - j)^n. Dynamic programming with a table is the most efficient approach for computing multiple values.

How are Stirling numbers related to Bell numbers?

Bell numbers are the row sums of the Stirling numbers of the second kind. The nth Bell number B(n) equals the sum of S(n, k) for k from 0 to n, representing the total number of ways to partition a set of n elements into any number of non-empty subsets. For example, B(4) = S(4,1) + S(4,2) + S(4,3) + S(4,4) = 1 + 7 + 6 + 1 = 15. Bell numbers grow very rapidly: B(10) = 115,975 and B(15) = 1,382,958,545. They can also be computed using the Bell triangle, a construction similar to Pascal triangle, and have applications in set theory, combinatorics, and computer science.

What are practical applications of Stirling numbers?

Stirling numbers appear in numerous practical applications across mathematics and computer science. In statistics, they are used for computing moments of distributions and in the theory of cumulants. In computer science, Stirling numbers of the second kind model the number of ways to distribute n distinct objects into k identical containers (like hash table analysis). They appear in the analysis of algorithms, particularly sorting and searching algorithms. In numerical analysis, they connect forward differences to derivatives. In combinatorial chemistry, they help count molecular configurations. They also appear in coding theory and in the enumeration of surjective functions between finite sets.

What is the recurrence relation for Stirling numbers of the first kind?

The unsigned Stirling numbers of the first kind satisfy the recurrence |s(n, k)| = (n - 1) * |s(n-1, k)| + |s(n-1, k-1)|, with base cases |s(0, 0)| = 1 and |s(n, 0)| = |s(0, k)| = 0 for positive n and k. The combinatorial interpretation is clear: when inserting element n into a permutation of {1, ..., n-1}, it either gets inserted into one of the existing cycles (n - 1 positions, one before each existing element) keeping the cycle count the same, or it forms a new fixed-point cycle by itself, increasing the cycle count by one. Signed Stirling numbers of the first kind include an alternating sign factor: s(n, k) = (-1)^(n-k) * |s(n, k)|.

References

Reviewed by Manoj Kumar, Mathematics Educator ยท Editorial policy