|
| 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 | + |
| 42 | + |
| 43 | +- **Test Case 2 :** |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +- **Test Case 3 :** |
| 48 | + |
| 49 | + |
| 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 | +[](https://www.python.org/) |
0 commit comments