Python Program for nth Catalan Number

In this article, we will learn about calculating the nth Catalan number using different approaches in Python.

Catalan numbers are a sequence of natural numbers that occur in various combinatorial problems. They are defined by the recursive formula:

C? = 1 and C??? = ????? C?C??? for n ? 0

The first few Catalan numbers for n = 0, 1, 2, 3, ... are 1, 1, 2, 5, 14, 42, 132, 429, ...

Catalan numbers can be calculated using recursion or dynamic programming. Let's explore both implementations.

Approach 1: Recursive Method

The recursive approach directly implements the mathematical definition ?

# A recursive solution
def catalan(n):
    # Base case
    if n <= 1:
        return 1
    
    # Catalan(n) = sum of catalan(i) * catalan(n-i-1)
    result = 0
    for i in range(n):
        result += catalan(i) * catalan(n-i-1)
    return result

# Calculate first 6 Catalan numbers
for i in range(6):
    print(catalan(i))
1
1
2
5
14
42

Approach 2: Dynamic Programming Method

The dynamic programming approach avoids redundant calculations by storing intermediate results ?

# Using dynamic programming
def catalan(n):
    if n == 0 or n == 1:
        return 1
    
    # Create table to store results
    dp = [0] * (n + 1)
    
    # Initialize base cases
    dp[0] = 1
    dp[1] = 1
    
    # Fill the table using bottom-up approach
    for i in range(2, n + 1):
        dp[i] = 0
        for j in range(i):
            dp[i] += dp[j] * dp[i-j-1]
    
    return dp[n]

# Calculate first 6 Catalan numbers
for i in range(6):
    print(catalan(i), end=" ")
1 1 2 5 14 42 

Performance Comparison

Let's compare the time complexity of both approaches ?

import time

def catalan_recursive(n):
    if n <= 1:
        return 1
    result = 0
    for i in range(n):
        result += catalan_recursive(i) * catalan_recursive(n-i-1)
    return result

def catalan_dp(n):
    if n == 0 or n == 1:
        return 1
    dp = [0] * (n + 1)
    dp[0] = 1
    dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = 0
        for j in range(i):
            dp[i] += dp[j] * dp[i-j-1]
    return dp[n]

# Test with n=15
n = 15

start = time.time()
result_recursive = catalan_recursive(n)
recursive_time = time.time() - start

start = time.time()
result_dp = catalan_dp(n)
dp_time = time.time() - start

print(f"Recursive: {result_recursive} (Time: {recursive_time:.4f}s)")
print(f"Dynamic Programming: {result_dp} (Time: {dp_time:.4f}s)")
Recursive: 9694845 (Time: 0.0234s)
Dynamic Programming: 9694845 (Time: 0.0001s)

Comparison

Method Time Complexity Space Complexity Best For
Recursive O(4?) O(n) Understanding the concept
Dynamic Programming O(n²) O(n) Efficient computation

Conclusion

Both methods calculate Catalan numbers correctly, but dynamic programming is significantly more efficient for larger values. Use the recursive approach for understanding the mathematical definition and the DP approach for practical applications requiring performance.

Updated on: 2026-03-25T06:23:34+05:30

650 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements