The Catalan Calculator

HardAcc. 75.6%
+40 XP 20

Counting Structur

Catalan numbers ($C_n$) appear in many counting problems, like "how many ways can we arrange $N$ pairs of parentheses?" Formula: $C_n = rac{1}{n+1} inom{2n}{n}$ or via the iterative product: $prod_{i=1}^n rac{n+i}{i}$.

The Assignment

Your function receives a parameter n.

  1. Initialize catalan to 1.
  2. Use a loop from 1 to n.
  3. In each step, calculate: catalan = catalan * (n + i) / i.
  4. Finally, divide by (n + 1) (though using the product formula $prod rac{n+k}{k}$ is often cleaner).
  5. Print the integer result (Math.round).

01EXAMPLE 1

Inputn = 3
Output5

Explanation: C0=1, C1=1, C2=2, C3=5.

Constraints

  • Use a single loop to compute the product.
  • Return a whole number.
MathLoopsCombinatorics
JavaScript
Loading...
3 Hidden

Input Arguments

n3

Expected Output

5

Click RUN to test your solution