forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rathoresrikant#756 from Felix1898/master
Added a C++ Program to Find nth Catalan Number
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// A dynamic programming based function to find nth | ||
// Catalan number | ||
unsigned long int catalan(unsigned int n) | ||
{ | ||
unsigned long int catlnno[n+1]; | ||
catlnno[0] = catlnno[1] = 1; | ||
for (int i=2; i<=n; i++) | ||
{ | ||
catlnno[i] = 0; | ||
for (int j=0; j<i; j++) | ||
{ | ||
catlnno[i] += catlnno[j] * catlnno[i-j-1]; | ||
} | ||
} | ||
|
||
return catlnno[n]; //Return nth Catalan No | ||
} | ||
|
||
int main() | ||
{ | ||
int n=10; | ||
cout << catalan(n) << " "; | ||
return 0; | ||
} | ||
|