ForHosting KIT · Math

Stirling numbers of the second kind

Count the ways to split items into unnamed, non-empty groups: 5 people form 2 such teams in 15 ways and 3 teams in 25 ways, S(5,2) and S(5,3). Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
N
5
K
2
You get
Value
15
Quantity
15

Stirling numbers of the second kind answer a fundamental counting question in discrete mathematics: given n distinct objects, in how many ways can you partition them into exactly k non-empty unlabeled groups? The answer is written S(n,k) or sometimes {n choose k} with curly braces. These numbers appear whenever you group people into teams without naming the teams, split a set of keys into unlabeled buckets, count surjective functions up to a factorial factor, build Bell numbers as a sum over k, or grade homework that asks for set partitions rather than permutations. This capability accepts non-negative integers n (the number of items) and k (the number of non-empty subsets) and returns the exact S(n,k) as a decimal string computed with BigInt dynamic programming, so values far beyond ordinary safe-integer limits stay correct digit for digit. Inputs with a negative k or with k greater than n are rejected with a clear validation error instead of a silent zero or a wrong answer.

How Stirling numbers of the second kind are defined

A Stirling number of the second kind S(n,k) counts the partitions of an n-element set into exactly k non-empty unlabeled subsets. Order inside a subset does not matter, and the subsets themselves are not ordered: the partition {{1,2},{3}} is the same as {{3},{1,2}}. The standard recurrence is S(n,k) = k · S(n−1,k) + S(n−1,k−1).

Intuitively, when you add the newest element n, either it forms a singleton of its own (then the remaining n−1 elements form k−1 subsets, counted by S(n−1,k−1)), or it joins one of the k existing subsets in a partition of the first n−1 elements into k blocks (k choices, times S(n−1,k)). Base cases that close the table are S(0,0) = 1, S(n,0) = 0 for n > 0, S(0,k) = 0 for k > 0, S(n,1) = 1, and S(n,n) = 1.

Classic textbook checks include S(3,2) = 3, S(4,2) = 7, S(5,2) = 15, and S(5,3) = 25. The Bell number B_n, the total number of partitions of an n-set with any number of blocks, is the sum of S(n,k) over k from zero to n. Related but different objects include Stirling numbers of the first kind (which track permutations by cycle count) and binomial coefficients (which choose elements without partitioning the rest).

This implementation never uses floating-point approximation, Monte Carlo sampling, or external tables: it fills a dynamic-programming row with exact BigInt arithmetic so every digit is correct. The path is fully deterministic—no random seeds, no clocks, no network, and no model drift—so the same n and k always yield the same string.

Where second-kind Stirling counts show up in real work

Combinatorics courses and programming contests constantly ask how many ways to split n labeled people into k unlabeled teams, how many equivalence relations on n elements have exactly k classes, or how many ways to distribute n distinct gifts into k identical non-empty boxes—all of which equal S(n,k). Algorithm designers use Stirling second-kind tables when analyzing set-partition data structures, clustering sketches with a fixed number of clusters, or counting the ways a recursive process can group intermediate results.

Probability and statistics courses connect S(n,k) to the number of ways to assign n distinct outcomes into k non-empty unlabeled bins and to the closed form for the number of surjections from an n-set onto a k-set, which is k! · S(n,k). Educators and interview prep often show a short triangle of small S(n,k) values; computing a single entry in one call keeps interactive demos aligned with textbooks and answer keys without hand-maintaining large tables.

If you later need Stirling numbers of the first kind, Bell numbers alone, surjection counts with the k! factor already applied, or an explicit listing of partitions rather than the count, treat those as separate search intents rather than overloading this landing.

Set-partition homework and contests

Compute exact S(n,k) for olympiad problems, discrete-math exercises, and automated answer keys that ask for unlabeled partitions into k blocks.

Surjection and clustering analysis

Size the partition factor behind surjective functions n→k and fixed-k clustering sketches without floating-point tables.

Bell-number building blocks

Generate individual second-kind entries when summing S(n,k) over k or validating Bell-number pipelines against textbook triangles.

What is a Stirling number of the second kind?

S(n,k) counts the ways to partition n distinct items into exactly k non-empty unlabeled subsets. Order inside subsets and among subsets does not matter.

How is this different from Stirling numbers of the first kind?

First-kind numbers count permutations by number of cycles. Second-kind numbers count set partitions by number of blocks. They answer different counting questions.

Why is the value a string instead of a number?

Stirling numbers grow past JavaScript safe integers quickly. Decimal strings keep every digit exact for large S(n,k).

What does it cost?

The browser tool is free and runs locally.

What happens if k is negative or larger than n?

The calculation is refused with a clear message instead of a silent zero. Use a number of groups between 0 and the number of items; both can go up to one thousand.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/combo/stirling-second

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/combo/stirling-second \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"n":5,"k":2}'
{
  "n": 5,
  "k": 2
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "combo.stirling_second",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

min_n0
max_n1000
min_k0
max_k1000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

The free browser tool and the paid API share one pure function, so classroom demos and automated pipelines never diverge on the same pair.

How Stirling numbers of the second kind are defined

Declared bounds keep n and k between zero and one thousand with the rule that k must not exceed n, which keeps worst-case latency suitable for a free in-browser tool and a lightweight Worker path while still covering contest, classroom, and pipeline needs for exact partition counts.

What you send and what you get back

The item count n must be an integer from zero through one thousand inclusive; the subset count k must be an integer from zero through one thousand inclusive and must satisfy k ≤ n. Error messages name the offending field and the rule that failed so form clients and scripts can correct the input without guessing. On success the response always includes the normalized n and k, value and count as the same exact decimal string for S(n,k), and a short formula string such as S(5,2) for display. Provide a required integer n under the field n (aliases items or size) and a required integer k under the field k (aliases subsets or parts). Missing fields, non-integers, decimals, negative n, negative k, and any k that exceeds n are rejected with invalid_input and are not charged. Example checks: n equals five and k equals two yields count fifteen; n equals five and k equals three yields count twenty-five; n equals three and k equals one yields count one; n equals four and k equals four yields count one; n equals zero and k equals zero yields count one; k equals six with n equals three yields invalid_input because k exceeds n; k equals negative one yields invalid_input. Pricing for successful API calls is the published base of $0.002 per item; validation failures do not consume prepaid balance.

Where second-kind Stirling counts show up in real work

Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.

What happens if k is negative or greater than n?

The request fails with invalid_input. This calculator requires 0 ≤ k ≤ n; negative k or k exceeding n is rejected and not charged.

Read the full KIT documentation →