The guide
Two ways to split four things: Stirling numbers, Bell numbers and what they count
Sorting labelled objects into groups and arranging them into cycles are different counts that share one family name. Each follows a short recurrence, and each can be checked object by object.
- 11 permutations of 4 with 2 cycles
- 1, 7, 6, 1 ways to split 4 into 1 to 4 groups
- 15 all splits of 4 objects
- 150 onto functions, 5 items to 3
- 116 digits length of B(100)
Read the full guide Fold the guide
Take four labelled objects, a, b, c and d. Put them into two non-empty groups and there are seven ways to do it. Arrange them in a permutation that has exactly two cycles and there are 11. Two counts, near-identical notation, different numbers.
That gap is the whole reason this family of numbers is easy to get wrong. The counts are named after the same person, written with the same letters and tabulated in triangles that look alike. They answer different questions.
Two counts with one family name
The Stirling number of the second kind, written S(n, k), counts the ways to split n labelled objects into k non-empty groups. The groups themselves are not labelled, so putting a and b together in one group and c and d in the other is the same split whichever group you name first. Another way to say it: S(n, k) counts the equivalence relations on n objects that have exactly k classes.
The unsigned Stirling number of the first kind, written c(n, k) here, counts permutations of n objects that have k cycles. A fixed point counts as a cycle of length one. For four objects, the permutations with two cycles come in two shapes: three of the form (a b)(c d), two pairs, and eight of the form (a b c)(d), a three-cycle with one object left alone. Three plus eight is 11.
The signed form carries a sign. It is the unsigned count times minus one raised to the power n minus k, so s(3, 2) is -3 while s(4, 2) is 11. The sign is nothing more than the parity of the gap between n and k, and forgetting it is a common slip.
The numbers take their name from the Scottish mathematician James Stirling. They are tabulated in triangles, one row for each n and one column for each k, with the boundary values S(0, 0) = 1 and c(0, 0) = 1.
Putting the last object somewhere
Both triangles are built from a rule that asks one question: where does the newest object go?
For splits, take a split of n + 1 objects into k groups and look at the last object. Either it sits alone, and the other n objects were split into k - 1 groups, or it shares a group, and the other n objects were already split into k groups with k choices of group to join. So S(n + 1, k) = k S(n, k) + S(n, k - 1). The recurrence goes back to Masanobu Saka in 1782.
For cycles the question is the same and the answer differs in one place. The new object either forms a cycle of its own, which gives c(n, k - 1), or it is slotted into an existing cycle right after one of the n objects already there. That is n places, not k, so c(n + 1, k) = n c(n, k) + c(n, k - 1).
Try it on the four-object example. S(4, 2) = 2 x S(3, 2) + S(3, 1) = 2 x 3 + 1 = 7. Then c(4, 2) = 3 x c(3, 2) + c(3, 1) = 3 x 3 + 2 = 11. One multiplier changed, from the number of groups to the number of objects already placed, and that single change moves the answer from 7 to 11.
The difference is easy to lose in a hurry, and swapping the multipliers is not a harmless slip. Swap them and the second-kind row for four objects, which should read 1, 7, 6, 1, comes out as 6, 11, 6, 1, which is the first-kind row. The two triangles trade places.
Rows for the same n tell different stories. The second-kind row for n = 4 is 1, 7, 6, 1 for k from 1 to 4. The first-kind row for n = 3 is 2, 3, 1. The first-kind row for n = 6 is 120, 274, 225, 85, 15, 1, and the second-kind row for n = 6 is 1, 31, 90, 65, 15, 1. They are not interchangeable.
Bell numbers add up a whole row
Add the second-kind row for n = 4 and you get 1 + 7 + 6 + 1 = 15. That is the Bell number B(4): the number of ways to split four objects into any number of groups. The Bell numbers begin 1, 1, 2, 5, 15, 52, 203, 877, 4140, and each is a row sum of the second kind.
The first kind has its own row-sum rule. Every permutation has exactly one number of cycles, so the first-kind row for n adds up to n factorial. Row 3 gives 2 + 3 + 1 = 6, which is 3 factorial.
The Bell numbers can be reached without any Stirling number at all. In the Bell triangle, start with a 1 on a row of its own. Each new row begins with the last number of the row above, and each further number is the one to its left plus the one above that one. The first number of row n is B(n). The first five rows run 1; 1, 2; 2, 3, 5; 5, 7, 10, 15; 15, 20, 27, 37, 52, so the left edge reads 1, 1, 2, 5, 15. The triangle is also called Aitken’s array or the Peirce triangle, and early references to it date from 1880 and 1933.
A third route uses binomial coefficients. Remove the group that holds the first object. The k objects that remain can be chosen in C(n, k) ways and split in B(k) ways, so B(n + 1) is the sum over k of C(n, k) B(k). The calculator runs all three routes and reports whether they agree.
Splits also count onto functions. A function from n items to k targets that reaches every target sorts the items into k non-empty groups, one for each target, and the groups can then be matched to the targets in k factorial ways. So the number of onto functions is k! S(n, k). From five items to three targets that is 3! x 25 = 150, out of the 243 functions that exist in total.
Numbers that outgrow the number type
These counts grow fast. B(100) has 116 digits, c(100, 50) has 112 digits and S(100, 50) has 102. The ordinary number type in a browser cannot hold values like that exactly. MDN describes BigInt as the type for integers too high or too low for the number primitive, and every figure on the page is a BigInt, built only from the recurrences with additions and small multiplications.
The page does the work in a Web Worker, which runs script in a background thread separate from the main thread so that the page stays responsive. At n = 200 the whole job still takes a few tens of milliseconds, and a cancel button ends any run that goes wrong.
Bell numbers also repeat when you look only at remainders. For a prime p, Touchard’s congruence says B(p + n) is congruent to B(n) + B(n + 1) modulo p. From that, the Bell numbers modulo p form a repeating cycle. For p = 2 the pattern is odd, odd, even. The period always divides (p^p - 1)/(p - 1), and for every prime up to 101 it equals that number. For p = 3 the period is 13, for p = 5 it is 781 and for p = 7 it is 137257. The page finds those directly by running the recurrence, and for larger primes it states the bound and says it did not compute the period.
What these numbers are for, and what the page leaves out
Set partitions turn up wherever labelled things are sorted into unlabelled bins, and every equivalence relation on a set is one. Cycle counts turn up in the structure of permutations. The unsigned first kind has a second meaning too, since it also counts permutations of n objects that have exactly k left-to-right maxima.
The two kinds are linked in a precise way. Seen as triangular matrices, the signed first kind and the second kind are inverses of one another. The page checks that for every pair of indices up to 25: the sum over k of S(i, k) s(k, j) is 1 when i equals j and 0 otherwise.
To reproduce the example, enter n = 4 and k = 2. The page returns S = 7, c = 11, s = 11, B(4) = 15 and 2! x 7 = 14 onto functions. For n up to 7 it also lists the seven partitions and the eleven permutations themselves, so the counts can be seen and not only trusted. Counting checks run on those lists: all 877 set partitions of seven objects and all 5040 permutations of seven objects are counted one by one and compared with the recurrence rows.
It has limits. It handles n up to 200, lists objects only up to n = 7, and shows at most 400 digits of a value on screen, with every digit in the exported files. It does not cover Stirling’s approximation of factorials, Eulerian numbers or generating-function work. For nPr and nCr it points to the Permutation and Combination Calculator, and for Catalan numbers to the Catalan Number Calculator. It computes exact counts. It does not say which counting question your problem is, and choosing between groups of labelled objects and cycles of a permutation is still up to the reader.
Sources
Every source consulted for this guide. Links checked November 10, 2026.
- Stirling numbers of the first kind - Wikipedia Wikipedia (CC BY-SA 4.0)
- Stirling numbers of the second kind - Wikipedia Wikipedia (CC BY-SA 4.0)
- Bell number - Wikipedia Wikipedia (CC BY-SA 4.0)
- Partition of a set - Wikipedia Wikipedia (CC BY-SA 4.0)
- DLMF: 26.8 Set Partitions: Stirling Numbers NIST Digital Library of Mathematical Functions (US government)
- DLMF: 26.7 Set Partitions: Bell Numbers NIST Digital Library of Mathematical Functions (US government)
- A008275 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A008277 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A132393 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A000110 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- A054767 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
- BigInt - JavaScript | MDN MDN Web Docs (Creative Commons licence)
- Web Workers API - Web APIs | MDN MDN Web Docs (Creative Commons licence)