Skip to content

Commit 38952c5

Browse files
Merge pull request #221 from abhisheks008/main
Nth Catalan Numbers using Dynamic Programming
2 parents b975982 + da64d08 commit 38952c5

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed
10.6 KB
Loading
9.96 KB
Loading
11 KB
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Nth Catalan Numbers using Dynamic Programming
2+
Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find out the n'th catalan numbers using Dynamic Programming.
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Dynamic programming on finding out the n'th catalan numbers.
9+
10+
## 📄 Description
11+
Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations.
12+
13+
**Examples:**
14+
```
15+
Input : k = 12
16+
Output : 1 1 2 5 14 42 132 429 1430 4862 16796 58786
17+
18+
Input : k = 10
19+
Output : 1 1 2 5 14 42 132 429 1430 4862
20+
21+
Input : k = 8
22+
Output : 1 1 2 5 14 42 132 429
23+
```
24+
25+
## 📈 Workflow of the script
26+
- `catalan` - The main function of the script to find out the catalan numbers using Dynamic Approach.
27+
- `main` - This is the driver code for this code/script.
28+
- `k` - User given integer which signifies the upper range of the Catalan series.
29+
30+
## 🧮 Algorithm
31+
Let's see how it works,
32+
- Create an array `catalan[]` for storing `ith` Catalan number.
33+
- Initialize, `catalan[0]` and `catalan[1]` = 1
34+
- Loop through `i = 2` to the given Catalan number `n`.
35+
- Loop throught `j = 0` to `j < i` and Keep adding value of `catalan[j] * catalan[i – j – 1]` into `catalan[i]`.
36+
- Finally, `return catalan[n]`.
37+
38+
## 💻 Input and Output
39+
- **Test Case 1 :**
40+
41+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Nth%20Catalan%20Numbers/Images/catalan-1.png)
42+
43+
- **Test Case 2 :**
44+
45+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Nth%20Catalan%20Numbers/Images/catalan-2.png)
46+
47+
- **Test Case 3 :**
48+
49+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Nth%20Catalan%20Numbers/Images/catalan-3.png)
50+
51+
52+
## ⏰ Time and Space complexity
53+
- **Time Complexity:** `O(n^2)`.
54+
- **Space Complexity:** `O(n)`.
55+
56+
---------------------------------------------------------------
57+
## 🖋️ Author
58+
**Code contributed by, _Abhishek Sharma_, 2022 [@abhisheks008](github.com/abhisheks008)**
59+
60+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Problem Name: Nth Catalan Numbers
2+
# Approach: Dynamic Programming
3+
4+
# -----------------------------------------------------------------------------------
5+
6+
# Problem Statement: Catalan numbers are defined as a mathematical sequence
7+
# that consists of positive integers, which can be used to
8+
# find the number of possibilities of various combinations.
9+
# Find out the catalan numbers in between the range from 0 to N.
10+
11+
12+
# -----------------------------------------------------------------------------------
13+
14+
# Constraints:
15+
# n -> Integer taken as the range of the Catalan Numbers.
16+
# catalan[] -> Array of Catalan Numbers from 0 to n.
17+
18+
# -----------------------------------------------------------------------------------
19+
20+
# A dynamic programming based function to find nth
21+
# Catalan number
22+
23+
def catalan(n):
24+
if (n == 0 or n == 1):
25+
return 1
26+
27+
# Table to store results of subproblems
28+
catalan = [0]*(n+1)
29+
30+
# Initialize first two values in table
31+
catalan[0] = 1
32+
catalan[1] = 1
33+
34+
# Fill entries in catalan[]
35+
# using recursive formula
36+
for i in range(2, n + 1):
37+
for j in range(i):
38+
catalan[i] += catalan[j] * catalan[i-j-1]
39+
40+
# Return last entry
41+
return catalan[n]
42+
43+
44+
# Driver Code
45+
print ("-- Nth Catalan Numbers using Dynamic Programming --")
46+
print ()
47+
print ("Enter the upper range : ")
48+
k = int(input())
49+
print ()
50+
print ("Printing Nth Catalan Numbers from 0 to {0}...".format(k))
51+
print ()
52+
print ("Here you go!")
53+
for i in range(k):
54+
print(catalan(i), end=" ")
55+
56+
57+
# -----------------------------------------------------------------------------------
58+
59+
# Output:
60+
# -- Nth Catalan Numbers using Dynamic Programming --
61+
62+
# Enter the upper range :
63+
# 12
64+
65+
# Printing Nth Catalan Numbers from 0 to 12...
66+
67+
# Here you go!
68+
# 1 1 2 5 14 42 132 429 1430 4862 16796 58786
69+
70+
# -----------------------------------------------------------------------------------
71+
72+
# Code Contributed by, Abhishek Sharma, 2022
73+
74+
# -----------------------------------------------------------------------------------

0 commit comments

Comments
 (0)