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

in #steemit6 years ago

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

EBOOK_PYTHON_no-name.png


Today i am going to show Programming Tip - How to calculate catalan numbers with the method of Binominal Coefficients using Pythonwith simple code. I hope you will understand this.
If you have any query you can ask.
i hope you will like it.


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))


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


Follow me at : https://steemit.com/@ahmadhassan
=========================================================


Thanks for reading and always welcome your suggestions :) =========================================================

Sort:  

This post has received a 1.92 % upvote from @booster thanks to: @ahmadhassan.

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by ahmadhassan from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62345.27
ETH 2427.57
USDT 1.00
SBD 2.49