Catalan Numbers
#include <bits/stdc++.h>
using namespace std;
unsigned long int catalan(unsigned int n){
//Base Case
if(n<=1)
return 1;
// catalan(n) is the sum of
// catalan(i)*catalan(n-i-1)
unsigned long int res = 0;
for(int i= 0;i<n;i++)
res += catalan(i) * catalan(n-i-1);
return res;
}
int main()
{
for(int i = 0;i<10;i++){
cout << catalan(i) << " ";
}
return 0;
}

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home