Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Weak Composition

How to use
  1. The ● Balls / A Letters switch at the left of the control bar draws every item as the same red ball or the same letter A; the bars look the same in both modes. Learn more about the mode switch
  2. The n stepper sets the number of identical items (3 to 5) and the k stepper the number of bins (2 to 4); each change clears the build and updates C(n+k−1,k−1)C(n + k - 1, k - 1) at the top right of the scene. Learn more about adjusting n and k
  3. Step ▶ drops one bar into its cell, ◀ Back undoes one step, ▶ Play builds every composition and stops at the end, ↺ Reset clears the completed section, and Speed sets how fast the bars fly. Learn more about the transport controls
  4. The top right of the scene shows the count C(n+k−1,k−1)C(n + k - 1, k - 1) and a status line such as x₁ = 0: 0 / 5. Learn more about getting started
  5. The strip has n+k−1n + k - 1 numbered cells; bars land in cells, coloured bands mark the bins, and the brackets below read x1,x2,…x_1, x_2, \dots as soon as the bars fix them, with a dashed mark for an empty bin. Learn more about the strip and bars
  6. Under the strip, composition: ( … ) ⇔ bar positions: { … } shows the same build in both encodings, and the formula card writes out the full count. Learn more about the composition and bar-position readouts
  7. The COMPLETED section files each strip in the row of its first-bin count x1x_1, from x1=0x_1 = 0 up, with the count of finished compositions at its right. Learn more about grouping by first-bin count
  8. The right panel adds one row per value of x1x_1, with a counter such as 0 / 5 and the formula for that row; a ✓ marks each finished row. Learn more about the right panel


Press ▶ Play to build all C(n+k−1, k−1) weak compositions of n identical items into k bins (empty bins allowed), or Step ▶ to drop one bar at a time. A bin is the run of cells between two adjacent bars (or before the first / after the last); the count of items in bin i is xᵢ. Each outcome = a choice of k − 1 bar positions among n + k − 1 total cells.
● BallsA Letters
n =4?
k =3?
Speed
C(6, 2) = 15Press Play or Step to beginBARS TO PLACE (k − 1 = 2)STRIP (n + k − 1 = 6 cells)1234561111x₁ = ?x₂ = ?x₃ = ?composition: (?, ?, ?)⇔bar positions: {?, ?}Choose k − 1 = 2 bar positions out of n + k − 1 = 6 cells:C(6, 2) = 6!/(2! · 4!) = 720/(2·24) = 15
Weak compositions (stars and bars)
C(n+k−1, k−1) = C(6, 2)
= 6! / (2! · 4!)
= 720 / (2 · 24)
= 15
Distribute n identical items into k distinct bins, with empty bins allowed. Count the solutions to x₁ + x₂ + … + xₖ = n in non-negative integers. Equivalently: choose k − 1 bar positions among n + k − 1 cells — the remaining cells hold the items.
The items are identical, so they carry no information — the bars do. Placing k − 1 dividers among n + k − 1 cells is the whole problem. Learn more about getting started · Adjusting n and k





Key Terms



Key Terms

Weak composition — a distribution of nn identical items into kk distinct bins where bins may be empty. Equivalently, the number of non-negative integer solutions to x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n. The count is (n+k−1k−1)\binom{n + k - 1}{k - 1}.

Stars and bars — the visual encoding of this problem. Draw nn stars in a row and insert k−1k - 1 bars among them to split them into kk groups. The count of arrangements is (n+k−1k−1)\binom{n + k - 1}{k - 1} because there are n+k−1n + k - 1 total symbols and we choose k−1k - 1 positions for the bars.

Bin — the run of cells between two adjacent bars in the strip, or before the first bar, or after the last. The number of items in bin ii is xix_i.

Strip — the row of n+k−1n + k - 1 cells where items (stars) and bars are placed. Each cell either holds one item or one bar.

Composition tuple (x1,x2,…,xk)(x_1, x_2, \dots, x_k) — the encoded outcome: xix_i is the count of items in bin ii, with x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n and each xi≥0x_i \geq 0.

group — the family of weak compositions with the same first-bin count. The completed section groups results by x1x_1; group sizes vary as (n−j+k−2k−2)\binom{n - j + k - 2}{k - 2} for x1=jx_1 = j.

Getting Started

The tool opens with n=4n = 4 identical items and k=3k = 3 bins. The scene splits into three areas:

• A holding row at the top labeled *BARS TO PLACE (k − 1 = K)* showing the k−1k - 1 bars waiting to be inserted.

• A strip in the middle labeled *STRIP (n + k − 1 = N+K-1 cells)* — a row of n+k−1n + k - 1 cells, numbered 11 to n+k−1n + k - 1 above. The nn items (identical, rendered as red balls or letter A) are pre-placed in the cells from the start. The animation moves the bars from the holding row into specific cell positions.

• A completed section below, where every finished composition is filed under the row matching its x1x_1 value.

To run the visualization:

• Press ▶ Play to auto-build all (n+k−1k−1)\binom{n + k - 1}{k - 1} weak compositions.

• Press Step ▶ to drop one bar at a time.

• Press ◀ to step backward.

• Adjust the Speed slider.

The header shows the formula C(n+k−1,k−1)=totalC(n + k - 1, k - 1) = \text{total} alongside a live status line like *x1=jx_1 = j: kk / size*.
C(6, 2) = 15Press Play or Step to beginBARS TO PLACE (k − 1 = 2)STRIP (n + k − 1 = 6 cells)1234561111x₁ = ?x₂ = ?x₃ = ?composition: (?, ?, ?)⇔bar positions: {?, ?}Choose k − 1 = 2 bar positions out of n + k − 1 = 6 cells:C(6, 2) = 6!/(2! · 4!) = 720/(2·24) = 15
(n, k) = (4, 3), idle, frozen

Four identical balls already seated in the strip; two bars waiting above. The items sit still — all the choice belongs to the dividers.

The frozen frame above is the opening position, and it reverses this section's usual casting: the items sit still — four identical red balls already in the strip — while the bars wait in the holding row above. The moving parts of a weak composition are the dividers, not the things being divided.

That inversion is the whole stars-and-bars insight in staging form: identical items carry no information, so all the choice lives in where the separators go. The bracket labels below the strip wait as question marks until the bars land.

The Strip and Bars

The strip is where the stars-and-bars encoding plays out:

• Numbered cells 11 through n+k−1n + k - 1 across the top. The current cell where a bar lands highlights to mark its position.

• Pre-placed items (red balls or the letter A) sit in cells that aren't currently bar positions. Items are identical — same color, same size — because the scenario treats them as indistinguishable.

• Bins are highlighted with alternating color bands that cover only the cells inside each bin (not the bar cells). Bin 1 might be blue-tinted, bin 2 amber, bin 3 blue again, and so on.

• Bars arrive from the holding row above with a dotted guide line tracing the trajectory.

• Brackets below the strip mark each bin with a label xi=jx_i = j. Brackets are progressive: each label shows ?? until the relevant bar lands, then it reveals the actual value.

• Empty bins appear as dashed mini-rectangles in the bracket row, indicating that no cells lie between adjacent bars.

When all k−1k - 1 bars are placed, a flash ring pulses around the strip and the composition is filed in the completed grid below.
C(6, 2) = 15x₁ = 0: 1 / 5BARS TO PLACE (k − 1 = 2)STRIP (n + k − 1 = 6 cells)1234561111x₁ = 0x₂ = ?x₃ = ?composition: (0, ?, ?)⇔bar positions: {1, ?}Choose k − 1 = 2 bar positions out of n + k − 1 = 6 cells:C(6, 2) = 6!/(2! · 4!) = 720/(2·24) = 15COMPLETED1 / 15x₁ =01111(0, 0, 4)
(4, 3) mid-build, frozen

The first bar landed at cell 1, sealing an empty first bin (x₁ = 0); the second rides its guide toward cell 3. The readout reads (0, 1, ?).

The frozen frame above catches the second composition mid-build: the first bar has landed at cell 1, pinning x1=0x_1 = 0 — an empty first bin, marked by the dashed mini-rectangle in the bracket row — while the second bar rides its guide toward cell 3. The readout has hardened to (0,1,?)(0, 1, ?).

Watch what each landing decides: a bar fixes the bin to its left, forever. That left-to-right hardening is why the tool can group results by x1x_1 the moment the first bar lands — the stars-and-bars encoding reads off prefix by prefix.

Composition and Bar-Position Readouts

Under the strip, two readouts restate the current build in both encodings, joined by ⇔:

• composition: ( … ) — the bin counts (x1,x2,…,xk)(x_1, x_2, \dots, x_k), zeros allowed.

• bar positions: { … } — the numbers of the cells that hold a bar.

Both start as question marks — (?,?,?)(?, ?, ?) and {?,?}\{?, ?\} for the default n=4n = 4, k=3k = 3 — and fill in as bars land. In the first build, one Step ▶ puts a bar in cell 1: (0,?,?)⇔{1,?}(0, ?, ?) \Leftrightarrow \{1, ?\}, an empty first bin. The second bar lands in cell 2: (0,0,4)⇔{1,2}(0, 0, 4) \Leftrightarrow \{1, 2\} — two bars side by side leave the second bin empty too, and the last bin takes all four items. The brackets under the strip show the same values.

Below the readouts, a formula card spells out the count for the current nn and kk: Choose k − 1 = 2 bar positions out of n + k − 1 = 6 cells, followed by C(6,2)=6!/(2!⋅4!)=720/(2⋅24)=15C(6, 2) = 6!/(2! \cdot 4!) = 720/(2 \cdot 24) = 15. The card changes only with the n and k steppers; the readouts change with every bar.

The two-way arrow is the stars-and-bars argument in one line: each set of k−1k - 1 bar cells gives exactly one composition, and each composition gives back exactly one set of cells.

Adjusting n and k

DemoChanging n and k
Step 0 of 5
Two steppers in the control bar control the size:

• n sets the number of identical items. Range 33 to 55.

• k sets the number of distinct bins. Range 22 to 44.

Common combinations:

• n=3,k=2n = 3, k = 2: (41)=4\binom{4}{1} = 4 compositions — one bar, four doors.

• n=3,k=3n = 3, k = 3: (52)=10\binom{5}{2} = 10 compositions.

• n=4,k=3n = 4, k = 3: (62)=15\binom{6}{2} = 15 compositions — the default configuration.

• n=4,k=4n = 4, k = 4: (73)=35\binom{7}{3} = 35 compositions.

• n=5,k=3n = 5, k = 3: (72)=21\binom{7}{2} = 21 compositions.

• n=5,k=4n = 5, k = 4: (83)=56\binom{8}{3} = 56 compositions — the largest run on this page.

Reducing nn does not constrain kk in this scenario, since even n=0n = 0 gives valid weak compositions (all xi=0x_i = 0). Changing either value resets the build, refreshes the formula in the header, and rebuilds the completed section into a new set of x1x_1-rows.

Grouping by First-Bin Count

DemoRows by first-bin count
Step 0 of 4
The completed section organizes weak compositions by x1x_1 — the count of items in the first bin. There are n+1n + 1 rows total (one for each value x1=0,1,2,…,nx_1 = 0, 1, 2, \dots, n), and group sizes vary.

When x1=jx_1 = j, the remaining n−jn - j items must be distributed among the remaining k−1k - 1 bins, also as a weak composition:

group size at x1=j=(n−j+k−2k−2)\text{group size at } x_1 = j = \binom{n - j + k - 2}{k - 2}


For example with n=4,k=3n = 4, k = 3:

• x1=0x_1 = 0: distribute 4 items into 2 bins. (51)=5\binom{5}{1} = 5 compositions.

• x1=1x_1 = 1: distribute 3 items into 2 bins. (41)=4\binom{4}{1} = 4 compositions.

• x1=2x_1 = 2: distribute 2 items into 2 bins. (31)=3\binom{3}{1} = 3 compositions.

• x1=3x_1 = 3: distribute 1 item into 2 bins. (21)=2\binom{2}{1} = 2 compositions.

• x1=4x_1 = 4: distribute 0 items into 2 bins. (11)=1\binom{1}{1} = 1 composition.

Total: 5+4+3+2+1=15=(62)5 + 4 + 3 + 2 + 1 = 15 = \binom{6}{2}. The decomposition is a visual proof of the hockey-stick identity ∑j=0n(n−j+k−2k−2)=(n+k−1k−1)\sum_{j=0}^{n} \binom{n - j + k - 2}{k - 2} = \binom{n + k - 1}{k - 1}.
C(6, 2) = 15Complete · 15 / 15BARS TO PLACE (k − 1 = 2)STRIP (n + k − 1 = 6 cells)1234561111x₁ = 4x₂ = 0x₃ = 0composition: (4, 0, 0)⇔bar positions: {5, 6}Choose k − 1 = 2 bar positions out of n + k − 1 = 6 cells:C(6, 2) = 6!/(2! · 4!) = 720/(2·24) = 15COMPLETED15 / 15x₁ =01111(0, 0, 4)1111(0, 1, 3)1111(0, 2, 2)1111(0, 3, 1)1111(0, 4, 0)x₁ =11111(1, 0, 3)1111(1, 1, 2)1111(1, 2, 1)1111(1, 3, 0)x₁ =21111(2, 0, 2)1111(2, 1, 1)1111(2, 2, 0)x₁ =31111(3, 0, 1)1111(3, 1, 0)x₁ =41111(4, 0, 0)
C(6, 2) complete, frozen

Fifteen strips in rows of 5, 4, 3, 2, 1 — a perfect countdown, one option lost for each item the first bin claims.

The frozen frame above is that decomposition completed: fifteen strip cards in five rows of 5,4,3,2,15, 4, 3, 2, 1 — a perfect countdown, because each extra item claimed by the first bin removes exactly one option from the leftover problem.

The triangular row profile is this scenario's fingerprint, as distinctive as the combination tool's staircase or the distribution tool's flat rows. One glance at how a completed grid's rows shrink tells you which counting regime produced it.

Three Bars, Fifty-Six Ways

The tool's largest setting: n=5n = 5 items, k=4k = 4 bins, so three bars land among 5+4−1=85 + 4 - 1 = 8 cells — (83)=56\binom{8}{3} = 56 weak compositions.

Fifty-six cards spread across six x1x_1-rows of sizes 21,15,10,6,3,121, 15, 10, 6, 3, 1 — the triangular numbers, marching down as the first bin absorbs more items. Each row is itself a complete run of the smaller problem "distribute the remainder into three bins."

At this scale the strip encoding shows its efficiency: every one of the 56 outcomes is a distinct picture of eight cells, yet the whole enumeration is just *choose 3 cells from 8* — a selection the combination tool could count without drawing a single strip.
C(8, 3) = 56Complete · 56 / 56BARS TO PLACE (k − 1 = 3)STRIP (n + k − 1 = 8 cells)1234567811111x₁ = 5x₂ = 0x₃ = 0x₄ = 0composition: (5, 0, 0, 0)⇔bar positions: {6, 7, 8}Choose k − 1 = 3 bar positions out of n + k − 1 = 8 cells:C(8, 3) = 8!/(3! · 5!) = 40320/(6·120) = 56COMPLETED56 / 56x₁ =011111(0, 0, 0, 5)11111(0, 0, 1, 4)11111(0, 0, 2, 3)11111(0, 0, 3, 2)11111(0, 0, 4, 1)11111(0, 0, 5, 0)11111(0, 1, 0, 4)11111(0, 1, 1, 3)11111(0, 1, 2, 2)11111(0, 1, 3, 1)11111(0, 1, 4, 0)11111(0, 2, 0, 3)11111(0, 2, 1, 2)11111(0, 2, 2, 1)11111(0, 2, 3, 0)11111(0, 3, 0, 2)11111(0, 3, 1, 1)11111(0, 3, 2, 0)11111(0, 4, 0, 1)11111(0, 4, 1, 0)11111(0, 5, 0, 0)x₁ =111111(1, 0, 0, 4)11111(1, 0, 1, 3)11111(1, 0, 2, 2)11111(1, 0, 3, 1)11111(1, 0, 4, 0)11111(1, 1, 0, 3)11111(1, 1, 1, 2)11111(1, 1, 2, 1)11111(1, 1, 3, 0)11111(1, 2, 0, 2)11111(1, 2, 1, 1)11111(1, 2, 2, 0)11111(1, 3, 0, 1)11111(1, 3, 1, 0)11111(1, 4, 0, 0)x₁ =211111(2, 0, 0, 3)11111(2, 0, 1, 2)11111(2, 0, 2, 1)11111(2, 0, 3, 0)11111(2, 1, 0, 2)11111(2, 1, 1, 1)11111(2, 1, 2, 0)11111(2, 2, 0, 1)11111(2, 2, 1, 0)11111(2, 3, 0, 0)x₁ =311111(3, 0, 0, 2)11111(3, 0, 1, 1)11111(3, 0, 2, 0)11111(3, 1, 0, 1)11111(3, 1, 1, 0)11111(3, 2, 0, 0)x₁ =411111(4, 0, 0, 1)11111(4, 0, 1, 0)11111(4, 1, 0, 0)x₁ =511111(5, 0, 0, 0)
C(8, 3) complete, frozen

Three bars among eight cells: 56 compositions in triangular rows of 21, 15, 10, 6, 3, 1 — Pascal’s diagonal drawn as cards.

The row sizes deserve their name: 21,15,10,6,3,121, 15, 10, 6, 3, 1 are consecutive triangular numbers, because each is itself a sum of the next row of counts — the hockey-stick identity stacked twice. Weak-composition counts are Pascal's triangle read along diagonals, and this grid is that diagonal drawn as cards.

Fifty-six is also where the identical-items assumption pays its dividend: the same five items distributed as distinct objects into four cells would give 45=10244^5 = 1024 outcomes. Forgetting who's who collapses a thousand cases into fifty-six — see related concepts for that comparison.

Transport Controls

DemoStep, file, back
Step 0 of 5
The control bar offers four transport buttons plus a speed slider:

• ◀ (Step back) — walks the animation one step backward.

• Step ▶ (Step forward) — drops one bar into its cell. Stop after each step to read the partial composition; cells without bars yet show ?? in their brackets.

• ▶ Play / ⏸ Pause — runs continuously until all (n+k−1k−1)\binom{n + k - 1}{k - 1} weak compositions are built, then auto-pauses.

• ↺ Reset — clears the completed section and starts over.

The Speed slider controls how fast play advances. At slower speeds you can clearly see each bar travel from the holding row into its cell and watch the bin brackets update from ?? to the actual count.

Mode Switch

DemoBalls and letters
Step 0 of 4
The Mode switch at the start of the control bar toggles how identical items are rendered:

• Balls mode (default) — items appear as red circles. All circles are identical because the items themselves are indistinguishable. Best for the classical stars-and-bars look.

• Letters mode — items appear as the letter A. Same identicality applies — every item shows the same letter in the same color. Best when reading compositions algebraically and emphasizing that the items are interchangeable.

The encoding is consistent across the strip, the mini cards in the completed grid, and the right-panel narration. Bars are unaffected — they always render as the same accent-colored vertical bars regardless of mode.

Right Panel and Progress

The right panel narrates the build as it unfolds, anchored by the header *Weak compositions (stars and bars)* with the full formula across multiple lines and a reminder that this counts non-negative integer solutions to x1+x2+⋯+xk=nx_1 + x_2 + \dots + x_k = n.

A StepRow is added for each x₁-group as soon as a composition in that group starts or completes. Each StepRow shows:

• The first bin count as a label — *First bin: x1=jx_1 = j*.

• A progress counter k/(n−j+k−2k−2)k / \binom{n - j + k - 2}{k - 2} tracking how many compositions in this group have completed.

• A short narration of the structure: *When the first bin holds jj items (x1=jx_1 = j), the remaining n−jn - j items are split among the other k−1k - 1 bins, giving (n−j+k−2k−2)\binom{n - j + k - 2}{k - 2} outcomes.* Edge cases get tailored phrasing: x1=0x_1 = 0 uses the first bin is empty; x1=nx_1 = n uses all items go to the first bin with the others empty.

When all groups complete, every StepRow shows done with a checkmark, and the counter reaches *total / total*.

What Is a Weak Composition

A weak composition of nn into kk parts is an ordered tuple (x1,x2,…,xk)(x_1, x_2, \dots, x_k) of non-negative integers summing to nn:

x1+x2+⋯+xk=n,xi≥0x_1 + x_2 + \dots + x_k = n, \quad x_i \geq 0


The word weak distinguishes this from a strong composition, which requires every xi≥1x_i \geq 1. Weak compositions allow zero parts, so the first bin (or any bin) is permitted to be empty.

The count of weak compositions is:

(n+k−1k−1)=(n+k−1n)\binom{n + k - 1}{k - 1} = \binom{n + k - 1}{n}


Examples:

• Distributing 1010 identical candies among 33 children (some children may get none): (122)=66\binom{12}{2} = 66 ways.

• Number of non-negative integer solutions to a+b+c+d=7a + b + c + d = 7: (103)=120\binom{10}{3} = 120.

• Choosing 55 scoops of ice cream from 66 flavors with repetition allowed (a flavor may be skipped): (105)=252\binom{10}{5} = 252 choices.

• Distributing nn identical items into kk labeled boxes with no capacity limit: the canonical setup.

On this page the idea runs live: from the opening strip, bars drop into numbered cells to carve the bins, results group into the triangular x₁ rows, and the n and k steppers scale the count from four compositions up to fifty-six.

For deeper coverage of weak compositions and their connections, see the weak composition section on the combinations theory page.

The Stars-and-Bars Argument

The classical proof maps weak compositions to a simple selection problem.

Encode each composition as a row of symbols. Write x1x_1 stars, then a bar, then x2x_2 stars, then a bar, and so on through xkx_k stars. The total is nn stars plus k−1k - 1 bars, giving n+k−1n + k - 1 symbols in a row.

Every weak composition is determined by where the bars sit. The bars partition the row into kk groups; the iith group has xix_i stars. Different bar placements give different compositions, and every composition comes from a unique bar placement (including bars adjacent to each other, which encode xi=0x_i = 0).

Count the bar placements. We're choosing k−1k - 1 positions out of n+k−1n + k - 1 total to be bars (the rest are stars). The count is the binomial coefficient:

(n+k−1k−1)\binom{n + k - 1}{k - 1}


This is the visual setup the tool implements. Each completed strip has nn items in fixed (canonical) positions and k−1k - 1 bars in specific cells; the cells without bars hold the items, and the brackets below convert the bar positions back into the tuple (x1,…,xk)(x_1, \dots, x_k).

The dual reading (n+k−1n)\binom{n + k - 1}{n} — choose the nn star positions instead of the bars — gives the same count by complementary counting.
C(5, 1) = 5Complete · 5 / 5BARS TO PLACE (k − 1 = 1)STRIP (n + k − 1 = 5 cells)123451111x₁ = 4x₂ = 0composition: (4, 0)⇔bar positions: {5}Choose k − 1 = 1 bar positions out of n + k − 1 = 5 cells:C(5, 1) = 5!/(1! · 4!) = 120/(1·24) = 5COMPLETED5 / 5x₁ =01111(0, 4)x₁ =11111(1, 3)x₁ =21111(2, 2)x₁ =31111(3, 1)x₁ =41111(4, 0)
C(5, 1) complete, frozen

One bar, five cells, five cards: with two bins, "choose the bar positions" stops being a metaphor and becomes pointable.

The frozen frame above shows the argument at its barest: k=2k = 2 bins means a single bar, and the five completed cards are simply the five cells that one bar can occupy among n+1=5n + 1 = 5. "Choose the bar positions" stops being a metaphor — you can point at each choice.

With one bar the binomial collapses to (51)=5\binom{5}{1} = 5, and the composition tuple is just (j,n−j)(j, n - j) — the bar's cell number decides everything. Every richer configuration is this picture with more bars to seat.