Dyck Path Counter

Enter a semilength n to count the Dyck paths of that size.

How it is calculated

The number of Dyck paths of semilength n is the Catalan number C(n) = (2n)! / (n! × (n+1)!), which is the same as C(2n, n) / (n+1). Instead of using large factorials, the tool builds it step by step with C(0) = 1 and C(k) = C(k−1) × 2(2k−1) / (k+1), using exact whole-number arithmetic. A path of semilength n has n up steps and n down steps, so 2n steps in total, which the tool also reports. Only whole numbers from 0 to 30 are accepted: C(30) = 3,814,986,502,092,304 is still exact, but larger values are not computed, and decimals, negatives or an empty field show no result. It also echoes your n.

Worked example

Take n = 4: four up steps and four down steps, 8 steps in all. Start from C(0) = 1. Then C(1) = 1 × 2 × 1 / 2 = 1, C(2) = 1 × 2 × 3 / 3 = 2, C(3) = 2 × 2 × 5 / 4 = 5, and C(4) = 5 × 2 × 7 / 5 = 14. So the tool shows 14 paths and 8 steps, which matches C(8, 4) / 5 = 70 / 5 = 14. Those 14 are exactly the 14 ways to write four pairs of parentheses correctly.

Try this example →

Common mistakes

  • Entering the total number of steps (8) instead of the semilength (4). The tool expects n, the number of up steps, and doubles it for the length.
  • Confusing Dyck paths with all up/down paths. Without the rule that the path never goes below the start, there would be C(2n, n) paths, which for n = 4 is 70, not 14.
  • Entering a decimal such as 2.5, or a negative n. Only whole numbers from 0 up are valid, and anything else gives no result.
  • Entering n above 30 and expecting a very large number. The tool stops at 30 to keep the count exact, so for bigger n you need a big-integer tool.
  • Assuming n = 0 gives zero. There is exactly one empty path, so the count for n = 0 is 1 (with 0 steps).

About this calculator

A Dyck path is a staircase-like lattice path made of "up" and "down" steps that starts and ends at the same height and never dips below its starting level — equivalent to a correctly balanced sequence of parentheses, where an "up" step is an opening bracket and a "down" step is a closing one. This calculator counts how many distinct Dyck paths exist for a given semilength n (n up-steps and n down-steps, 2n steps total), a count that equals the nth Catalan number, C(2n,n)/(n+1).

Catalan numbers via Dyck paths show up constantly in combinatorics and computer science: they count the ways to correctly match parentheses in an expression, the number of distinct binary search trees with n nodes, the ways to triangulate a convex polygon, and valid push/pop sequences on a stack — which is why the same C(2n,n)/(n+1) formula keeps reappearing across seemingly unrelated counting problems in discrete math courses.

Was this helpful?

You can pick up to 3 reactions.

Comments

  • Be the first to comment.

Popular calculators

All Calculators