MathsNumber Theory

Catalan Number
calculator.

Count balanced brackets, mountain paths, binary trees and polygon cuts for any n.

How it's worked out

The Catalan numbers start 1, 1, 2, 5, 14, 42, 132, and the term for n is written CnC_n. OEIS A000108 gives the closed form. The top, a binomial coefficient, is the middle entry of row 2n of Pascal's triangle.

Cn\displaystyle C_n=1n+1(2nn)\displaystyle {}= \frac{1}{n+1}\binom{2n}{n}
Cn\displaystyle C_n=(2n)!n!(n+1)!\displaystyle {}= \frac{(2n)!}{n!\,(n+1)!}

Segner's recurrence builds each term from smaller ones. Take a balanced string of brackets and find where the first bracket closes. Inside it is a balanced string of k pairs, and after it is one of n − 1 − k pairs. Add up the products over every k. For n = 5 that gives 1 × 14 + 1 × 5 + 2 × 2 + 5 × 1 + 14 × 1 = 42.

Cn+1\displaystyle C_{n+1}=∑k=0nCkCn−k\displaystyle {}= \sum_{k=0}^{n} C_k \, C_{n-k}

The page itself uses a faster step from one term to the next, one multiply and one exact divide each time. That is how it reaches n = 1,000 with every digit.

Cn+1\displaystyle C_{n+1}=2(2n+1)n+2Cn\displaystyle {}= \frac{2(2n+1)}{n+2} \, C_n

The check uses paths. A path of n up-steps and n down-steps can be chosen in (2nn)\binom{2n}{n} ways. A reflection trick shows that (2nn+1)\binom{2n}{n+1} of them dip below the start. Take those away, and what is left is CnC_n. For n = 5 that is 252 − 210 = 42.

For large n the terms grow close to 4n4^n divided by n3/2πn^{3/2}\sqrt{\pi}. This estimate runs a little high. At n = 10 it is 11.38% over, and at n = 100 it is 1.13% over.

Worked examples

Sam lists every way to close three pairs of brackets

Sam is writing a test for a bracket checker and wants every valid input with three pairs. There are C3=5C_3 = 5 of them: ((())), (()()), (())(), ()(()), ()()().

The closed form agrees. The middle of row 6 of Pascal's triangle is 20, and 20 over 4 is 5. Of the 20 strings with three of each bracket, 15 close a bracket too early, which leaves 5.

Open this example in the tool

Ana cuts a hexagon into triangles

A hexagon has 6 sides, which is n + 2 with n = 4, so Ana needs C4C_4. Each way uses 3 diagonals and makes 4 triangles. There are 14 ways.

To see the recurrence, she fixes one edge. The triangle on that edge splits what is left into two smaller polygons, and that gives 1 × 5 + 1 × 2 + 2 × 1 + 5 × 1 = 14. Euler worked on this count in 1751, in a letter to Goldbach.

Open this example in the tool

Kenji finds the last Catalan number a 64-bit integer holds

Kenji's program counts binary trees with a signed 64-bit integer, whose top value is 9,223,372,036,854,775,807. The largest Catalan number at or below that is C35C_{35} = 3,116,285,494,907,301,262.

The next term, C36C_{36} = 11,959,798,385,860,453,492, overflows. With 32-bit integers the limit comes much sooner, at C19C_{19} = 1,767,263,190. So for n past 35 he switches to BigInt.

Open this example in the tool

Reading your result

Four readings of one number. The same CnC_n counts four things. It counts strings of n balanced bracket pairs. It counts mountain paths of n up-steps and n down-steps that stay at or above the start. It counts binary trees with n nodes, where each node has a left and a right branch that may be empty. And it counts ways to cut a polygon with n + 2 sides into triangles. Stanley's book on the subject lists 214 such readings.

Brackets and paths are the same thing. Read an open bracket as a step up and a close bracket as a step down. A string is balanced exactly when its path never dips below the line. For a tree, the part inside the first pair of brackets is the left branch, and the rest is the right branch.

The ratio to the next term is 2(2n + 1) over n + 2. It climbs toward 4 but never reaches it, so each term is a bit less than four times the one before. That is why C100C_{100} already has 57 digits and C1,000C_{1,000} has 598.

Common mistakes

  • Using n for the number of sides. A polygon with n sides has Cn−2C_{n-2} cuts, not CnC_n. A hexagon gives C4=14C_4 = 14, not C6C_6.
  • Mixing up pairs and letters. Putting brackets into a product of n + 1 letters, such as abcd for n = 3, also gives CnC_n. So 4 letters give 5 ways, not 14.
  • Working out the factorials in full. (2n)!(2n)! overflows a 64-bit integer long before CnC_n does, since C35C_{35} still fits. The step from one term to the next keeps every value close to the answer's size.
  • Using it for unequal vote counts. CnC_n answers the ballot problem only when both sides get n votes. With unequal counts you need the general ballot numbers, which this page does not compute.

Questions people ask

Exactly the nth Catalan number: 1 for one pair, 2 for two, 5 for three and 16796 for ten. A program that builds them all will list that many, so the count is a quick test for code that generates them.

Sources