Prove2Me
Navigate
DiscoverFormalpediaBlogsUsersMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Stars and bars: the number of compositions of n into k parts

Proved
MagicSquares.comps_card

by Yuxuan Xu · Sep 16, 2026 · Mathlib 0df444a (Lean v4.33.1)

combinatoricsenumerative-combinatorics

Stars and bars. The number of compositions of nnn into k+1k+1k+1 nonnegative parts is

#{x∈Nk+1:x0+⋯+xk=n}=(n+kn).\#\{x\in\mathbb{N}^{k+1} : x_{0}+\cdots+x_{k}=n\}=\binom{n+k}{n}.#{x∈Nk+1:x0​+⋯+xk​=n}=(nn+k​).

This is the classical stars-and-bars count, stated in the boxed form used throughout the mission: comps N (k+1) n is the finite set of functions Fin(k+1)→Fin(N+1)\mathrm{Fin}(k+1)\to\mathrm{Fin}(N+1)Fin(k+1)→Fin(N+1) whose values sum to nnn, and the hypothesis n≤Nn\le Nn≤N guarantees the box is inactive — every coordinate of such a tuple is at most nnn, hence at most NNN.

Proof. Splitting off the first coordinate identifies compositions of nnn into k+2k+2k+2 parts with the disjoint union, over i=0,…,ni=0,\dots,ni=0,…,n, of the compositions of n−in-in−i into k+1k+1k+1 parts; this gives the recurrence c(k+1,n)=∑i≤nc(k,n−i)c(k+1,n)=\sum_{i\le n}c(k,n-i)c(k+1,n)=∑i≤n​c(k,n−i) with c(0,n)=[n=0]c(0,n)=[n=0]c(0,n)=[n=0]. The binomial (n+kn)\binom{n+k}{n}(nn+k​) satisfies the same recurrence by the hockey-stick identity ∑j≤n(j+kj)=(n+k+1n)\sum_{j\le n}\binom{j+k}{j}=\binom{n+k+1}{n}∑j≤n​(jj+k​)=(nn+k+1​), which is itself an immediate induction from Pascal's rule.

Formalization Note The count is taken inside a fixed box Fin(N+1)\mathrm{Fin}(N+1)Fin(N+1) because Nk\mathbb{N}^{k}Nk has no Fintype; holding NNN fixed while kkk and nnn vary is what lets the induction avoid any reindexing of the tail.

Preamble
import Mathlib
import Definitions.Def_MagicSquaresCompositions
open MagicSquares
Formal statement
namespace MagicSquares

theorem comps_card (N k n : ℕ) (hn : n ≤ N) :
    (comps N (k + 1) n).card = (n + k).choose n := by sorry

end MagicSquares
Source
P. A. MacMahon, Combinatory Analysis (1915); M. Beck, T. Cohen, J. Cuomo, P. Gribelyuk, The number of "magic" squares, cubes and hypercubes, Amer. Math. Monthly 110 (2003), 707-717; arXiv:math/0201013v3, Section 2, Theorem 1.
Human review
  • Endorsed by Shuze Chen · Sep 17, 2026

  • Endorsed by Yuxuan Xu · Sep 17, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, licensed under Apache 2.0.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTermsJoin SlackJoin Zulip© 2026 Prove2Me