How to calculate catalan numbers with the method of Binominal Coefficients using Python?


To calculate Catalan numbers using binomial Coefficients, you first need to write a function that calculates binomial coefficients. 

example

def binomialCoefficient(n, k):
   # To optimize calculation of C(n, k)
   if (k > n - k):
      k = n - k
   coeff = 1
   for i in range(k):
      coeff *= (n - i)
      coeff /= (i + 1)
   return coeff

def catalan(n):
   return binomialCoefficient(2*n, n) / (n + 1)

for i in range (11):
   print (catalan(i))

Output

This will give the output −

1.0
1.0
2.0
5.0
14.0
42.0
132.0
429.0
1430.0
4862.0
16796.0

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 05-Mar-2020

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements