Stirling Number Calculator - First & Second Kind Tool

Labelled and distinguishable, 0 to 100

Every block must be non-empty, and the blocks are unordered

Four into twoThe textbook starter. S(4,2) = 7, and all seven partitions are small enough to list in full below.

Partitions of 4 labelled items into 2 non-empty blocks

S(4, 2) = 7

S(4, 2)

7

set partitions into exactly k blocks

Bell number B(4)

15

the whole row added up — partitions into any number of blocks

Onto functions to 2 values

14

k! × S(n,k) — label the blocks and you get surjections

Share of the row

46.67%

peak sits at k = 2 — you are on it

All three Stirling numbers at n = 4, k = 2

NotationValueWhat it counts
S(4,2)7Ways to split the set into k unlabelled non-empty blocks
c(4,2)11Permutations with k disjoint cycles — blocks plus their cyclic order
s(4,2)11Same magnitude, sign (−1)42 = +1

c ÷ S = 1.57× — the first kind is never smaller, because every block of size m can be wound into (m−1)! different cycles, and only blocks of size 1 or 2 have just one.

Built from the row above

S(4,2) = 2 × S(3,2) + S(3,1)

= 2 × 3 + 1 = 7

Item 4 has two futures. It joins one of the 2 blocks already built from the first 3 items — 2 choices, hence the multiplier — or it opens a block of its own, which needs the earlier items to have filled only 1 blocks. Nothing is double counted because the two cases disagree about whether item 4 is alone.

Stirling triangle, second kind — the target cell is green, the two cells that build it are amber

n \ k01234
01····
101···
2011··
30131·
401761

Unlike Pascal’s triangle the rows are not symmetric: the left edge is always 1 and so is the diagonal, but the bulge sits left of centre.

How the 15 partitions of a 4-element set divide by block count

The tallest bar is the most common outcome; your k is highlighted.

0
0
1
6.67%
2
46.67%
3
40.00%
4
6.67%

The peak at k = 2 keeps drifting right as n grows — it tracks n / W(n) for the Lambert function W, comfortably above the n / ln(n) most people expect. A random partition of a large set has far more blocks than intuition suggests.

The closed form, term by term — inclusion–exclusion over which of the 2 blocks stay empty

jC(2,j)(2−j)4(−1)j C(2,j) (2−j)4
011616
121-2
2100
Sum — the number of onto functions from 4 items to 2 labelled values14
Divide by 2! = 2 to unlabel the blocksS(4,2) = 7

Every one of the 16 functions from the set to 2 labelled values is counted, then the ones missing a value are subtracted back out, then the double-subtractions are added in again. The alternating signs are the whole mechanism, and the final division by 2! is what turns labelled boxes into unlabelled blocks.

Every partition, written out — all 7

Blocks are unordered, so {1,2}{3} and {3}{1,2} are the same partition and appear once.

{1,2,3} {4}{1,2,4} {3}{1,2} {3,4}{1,3,4} {2}{1,3} {2,4}{1,4} {2,3}{1} {2,3,4}

How to Use This Calculator

  1. Put the size of your set in n and the number of groups in k. Both accept 0, and S(0,0) = 1 is a real answer rather than an error.
  2. Pick a family under Which Stirling number. Choose the second kind if you are splitting a set into groups, the first kind if you are counting permutations by their cycles, and the signed version only if a formula in front of you shows a minus sign.
  3. Read the all three Stirling numbers table to see how far apart the two families are at your n and k — that gap is the single most common source of a wrong answer.
  4. Use the triangle to check homework by hand: the green cell equals the multiplier times the amber cell above it plus the amber cell to that one’s left.
  5. For small cases the calculator lists every partition or cycle decomposition in full, so you can verify the count by eye instead of trusting it.

Share this calculator

Help others solve their calculations

Found this calculator helpful? Share it with your friends, students, or colleagues who might need it!

Stirling Number Calculator: Blocks, Cycles, and Which Kind You Need

About the Author

Marko Šinko - Co-Founder & Lead Developer

Marko Šinko

Co-Founder & Lead Developer, AI Math Calculator

Lepoglava, Croatia
Advanced Algorithm Expert

Croatian developer with a Computer Science degree from University of Zagreb and expertise in advanced algorithms. Co-founder of award-winning projects, ensuring precise mathematical computations and reliable calculator tools.

📅 Published:
Stirling Number Calculator showing a triangle of S(n,k) values beside four labelled dots being grouped into two unlabelled blocks

Any Stirling number calculator has to settle one question before it can give you a number: which kind? At n = 4 and k = 2 the second kind says 7 and the first kind says 11, and both are correct — they are counting different things. The second kind, written S(4,2), counts the ways to split four labelled items into two unlabelled groups. The first kind, c(4,2), counts the permutations of four items that break into exactly two cycles.

Almost everyone who searches for this arrives wanting the second kind, because that is the one that shows up in set partitions, surjections, Bell numbers and the standard combinatorics curriculum. The first kind turns up later, in falling factorials and in the analysis of shuffles. Get the two mixed up and every downstream number is wrong by a factor that grows with n.

The Seven Ways to Split Four People

Take four people — 1, 2, 3 and 4 — and put them into two non-empty teams. The teams have no names, no captain, no order. Here is the complete list:

Shape 3 + 1 — four of them

{1,2,3}{4}{1,2,4}{3}{1,3,4}{2}{2,3,4}{1}

Shape 2 + 2 — three of them

{1,2}{3,4}{1,3}{2,4}{1,4}{2,3}

Seven altogether, and the arithmetic on the 2+2 group is the interesting half: choosing who joins person 1 gives three options, and that is the entire count — because once you name person 1’s partner, the other pair is forced. Trying to compute it as “choose 2 of 4” gives 6, which double counts every split. That factor of 2 is the first place a combination calculator will lead you astray here: nCr counts labelled selections, and Stirling numbers of the second kind do not label the blocks.

The 3+1 group is the reverse case — there, choosing which single person sits alone gives 4 with no correction needed, because the two blocks have different sizes and so cannot be confused with each other. Equal block sizes are exactly when the division kicks in, and that asymmetry is why no single binomial expression covers S(n,k).

One Decision Per Element Builds the Whole Triangle

The recurrence is the tool that actually gets used, and it comes from asking one question about the last element:

S(n,k) = k · S(n−1,k) + S(n−1,k−1)

c(n,k) = (n−1) · c(n−1,k) + c(n−1,k−1)

For the second kind: element n is either alone in its own block, in which case the other n−1 elements formed k−1 blocks, or it joined a block that already existed, in which case the other n−1 elements formed all k blocks and there were k of them to choose from. Two disjoint cases, no overlap, and S(4,2) = 2·S(3,2) + S(3,1) = 2·3 + 1 = 7 lands on the seven partitions listed above.

The first kind uses the same skeleton with a different multiplier, and that difference is worth a moment. Element n can be inserted immediately after any of the n−1 earlier elements inside whatever cycle that element sits in — n−1 slots, not k. So the first kind grows faster, and the gap widens with n rather than staying constant:

nS(n,2)c(n,2)Ratio c/S
3331.0
47111.6
6312748.8
105111,026,5762,009

At n = 3 the two families agree, which is why a homework problem checked only against small cases can hide the error completely. By n = 10 the first kind is two thousand times larger. Both triangles are laid out in the calculator above with the target cell and its two parent cells highlighted, so the recurrence is something you can trace rather than trust.

Blocks or Cycles: Telling the Two Kinds Apart

The cleanest way to see the difference is to take one set partition and count how many permutations it corresponds to. Split {1,2,3,4} as {1,2,3}{4}. As a partition that is one object. As a permutation you also have to decide how the block of three cycles: 1→2→3→1 or 1→3→2→1. Two cyclic orders, so one partition becomes two permutations. A block of size m admits (m−1)! cyclic orders — which is where the factorial hiding inside the first kind comes from.

Run that over the whole list: the four partitions of shape 3+1 each give 2 permutations, and the three of shape 2+2 each give 1 (a 2-cycle has only one cyclic order). Total 4×2 + 3×1 = 11, which is c(4,2) exactly. That is not a coincidence or an identity to memorise — it is what the first kind means.

There is also a third notation in circulation, and it trips people up in print rather than in counting. The signed Stirling numbers of the first kind, s(n,k) = (−1)n−k c(n,k), are the coefficients that expand a falling factorial into ordinary powers. Nothing is being counted by a negative number; the sign is bookkeeping so the algebra comes out right. If a source writes s(4,2) = 11 and another writes s(4,2) = −11, both are using a standard convention and neither is wrong.

Which Balls-in-Boxes Question Are You Asking?

Most wrong answers in this area are not arithmetic errors — they are the wrong question. Four different counts hide behind “put n things into k groups”, depending on whether the things and the groups are distinguishable. This table is the one worth keeping:

ItemsGroupsCountn=4, k=2
DistinctIdenticalS(n,k)7
DistinctLabelledk! · S(n,k)14
IdenticalIdenticalpartitions of n into k parts2
IdenticalLabelledC(n−1, k−1)3

Same n, same k, four answers between 2 and 14. The third row — identical items into identical groups — is 2 because only the sizes matter: 3+1 or 2+2. The fourth row is the stars-and-bars count, 3 because you are choosing where to cut a row of four identical balls into two non-empty runs. Only the top row is a Stirling number of the second kind.

A practical test: if renaming the groups changes your answer, you want a labelled row. Handing four different tasks to two identical anonymous committees is the top row. Handing them to Committee A and Committee B is the second. This is the same distinction that separates a permutation calculator from a combination calculator, one level up: it is not the items being ordered, it is the containers.

Onto Functions Are Just k! × S(n,k)

Row two of that table is where Stirling numbers of the second kind earn their place in computer science. A surjection from an n-element set onto a k-element set is precisely a partition into k blocks plus an assignment of the blocks to the k target values, so the count is k! · S(n,k). For n = 4, k = 2 that is 2 · 7 = 14 onto functions — and you can sanity check it directly: 24 = 16 functions total, minus the 2 constant ones that miss a value, gives 14.

Turn that around and you get the closed form the calculator above traces term by term:

S(n,k) = (1/k!) · Σj=0..k (−1)j C(k,j) (k−j)n

Read it as inclusion–exclusion over which target values go unused. Count all kn functions, subtract the ones that miss at least one value, add back the ones double-subtracted for missing two, and so on. Then divide by k! to forget the labels. For S(4,2): (1/2)·(24 − 2·14 + 1·04) = (16 − 2)/2 = 7.

This formula is exact but a poor way to compute anything large. It needs k+1 terms of alternating sign with the biggest one around kn, so in floating point the cancellation destroys precision fast — by around n = 20 a double-precision implementation is returning nonsense for mid-range k. The recurrence has no subtraction at all for the second kind, which is why every serious library builds the triangle instead. Related sequence tools run into the same trade-off; the Catalan number calculator has a clean product formula, but a recurrence relation calculator is the more robust route once the numbers stop fitting in a double.

Where a Stirling Number Calculator Beats Hand Work

Building the triangle by hand is fine up to about n = 8. After that the numbers get long and the errors compound silently, since one wrong cell corrupts every cell below and to the right of it. Some landmarks for scale:

  • S(10,5) = 42,525 — still a plausible hand computation, ten rows of arithmetic.
  • B(10) = 115,975 — the Bell number, the whole row added up: every way to partition ten items into any number of groups.
  • B(20) ≈ 5.17 × 1013 — more partitions of a 20-element set than there are seconds in 1.6 million years.
  • S(50,12) is a 46-digit number — exact integer arithmetic is not optional here, and a spreadsheet using doubles will silently lose the low digits.

The calculator computes with arbitrary-precision integers for exactly that reason, and it shows the row distribution because the shape is genuinely surprising. Split a 50-element set at random and the most likely number of blocks is not 2 or 3 but 16. The peak of S(n,k) tracks n / W(n), where W is the Lambert function — about 17.5 at n = 50, and noticeably higher than the n / ln n figure of 12.8 that a quick estimate suggests. The first kind peaks near ln n instead: a random shuffle of 52 cards splits into 4 cycles more often than any other number, and 4.54 on average, rather than one big loop.

Four Slips That Produce the Wrong Stirling Number

Treating S(0,0) as 0. It is 1. There is exactly one way to partition the empty set: use no blocks. Every recurrence in the subject depends on this base case, and an implementation that returns 0 there produces a triangle that is wrong in its entirety. The same goes for c(0,0) = 1. But S(n,0) = 0 for every n above 0, which looks inconsistent until you read it as “no blocks cannot hold anything”.

Multiplying by k! when the groups are anonymous. This is the single most common error, and it is easy to catch: ask whether swapping the contents of group 1 and group 2 gives you a different arrangement. If it does not, no k! belongs in the answer. Sorting four students into two unnamed study pairs is 3 ways, not 6.

Confusing set partitions with integer partitions. “Partitions of 4” means 5 (4, 3+1, 2+2, 2+1+1, 1+1+1+1) when the items are identical, and 15 — the Bell number B(4) — when they are distinct. A set calculator works on labelled elements, which is the Stirling case; integer partitions ignore identity entirely.

Reading a signed table as a count. If a table shows −225 at n = 6, k = 3, that is s(6,3) with the (−1)n−k convention attached, and the count is 225. Signed values appear in generating function work and in falling-factorial expansions; unsigned values appear whenever something is being counted. The standard reference on both families spells out the conventions, and the sequences themselves are catalogued as OEIS A008277 for the second kind and A132393 for the unsigned first kind — useful for checking any row you have computed by hand against a published table.

Frequently Asked Questions

Still Have Questions?

The detailed content on this page provides comprehensive explanations and examples to help you understand better.