Hello all mighty hackers, mathematicians and coders!
I work hard to create a recursive algorithm for the Catalan numbers equation below
C(n) =∑ C(i−1) C(n−i) (and simplification of this equation using stirling numbers or other forms is not an option..)
Here is the recursive algorithm so far:
int Cval[1024];//1024 just for example
int C( int n )
{
if( Cval[n] ) != 1 ) return Cval[n];
int ans = 0;
if( n == 0 ) ans = 1;
for( int i = 1; i <= n; i++ )
ans += C( i - 1 ) * C( n - i );
return Cval[n] = ans;
}
int main()
{
for( int i = 0; i < 1024; i++ ) Cval[i] = 1;
// call C(n) for any n up to 1023
}
Now, I'm trying to convert this to an iterative algorithm.. And I need your precious help ;) Any ideas?