Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Catalan number

"""
This implementation demonstrates how
to efficiently calculate the nth
Catalan number. For instance, 
the nth catalan number gives the 
number of unique binary search trees 
which has exactly n nodes of unique values
between 1 and n.

Let us denote it by Cn:
C0 = 1 and Cn+1 = 2(2n+1)Cn/(n+2)

More info at:
https://en.wikipedia.org/wiki/Catalan_number

Time complexity: O(n)
Space complexity: O(1)
"""


def C_n(n):
    C = 1
    for i in range(0, n):
        C = C * 2*(2*i+1)/(i+2)
    return int(C)


print("C1 =", C_n(1))  # C1 = 1
print("C3 =", C_n(3))  # C3 = 5
Comment

PREVIOUS NEXT
Code Example
Python :: Python3 boto3 put and put_object to s3 
Python :: tkinter button hide 
Python :: python read and write pdf data 
Python :: how to import opencv in python 
Python :: userregisterform 
Python :: python check if website is reachable 
Python :: python write text file on the next line 
Python :: how to get int input in python 
Python :: types of dict comprehension 
Python :: python isinstance list 
Python :: print(hello world) 
Python :: django in conda 
Python :: django response headers 
Python :: python read file into variable 
Python :: if name 
Python :: django migrate model 
Python :: python dequeu 
Python :: python split string size 
Python :: python string startswith regex 
Python :: how to extract words from string in python 
Python :: How to create DataFrames 
Python :: how to loop through string in python 
Python :: extract email address using expression in django 
Python :: Game of Piles Version 2 codechef solution 
Python :: How to create role discord.py 
Python :: python random number guessing game 
Python :: discord bot slash 
Python :: Iterate through characters of a string in python 
Python :: time.strftime("%H:%M:%S") in python 
Python :: pd df rename 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =