forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateCatalanNo.cpp
More file actions
41 lines (35 loc) · 793 Bytes
/
generateCatalanNo.cpp
File metadata and controls
41 lines (35 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Complexity analysis
// catalan number grows exponentially
// Time complexity of following program is O(n)
#include <iostream>
using namespace std;
// / Returns value of Binomial Coefficient C(n, k)
unsigned long int binomialCoeff(unsigned int n, unsigned int k)
{
unsigned long int res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
unsigned long int catalan(unsigned int n)
{
// Calculate value of 2nCn
unsigned long int c = binomialCoeff(2*n, n);
// return 2nCn/(n+1)
return c/(n+1);
}
int main(){
int n;
cout<<"Enter a number:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<catalan(i)<<" ";
}
cout<<endl;
}