Saturday, January 19, 2008

Factorial n!

Computing n!

There are many ways to compute n!, but I will introduce a divide and conquer method.
Using these rules below:
f(n,m) = 1 if n == m,
f(n,m) = 0 if n > m,
f(n,m) = f(n,x) * f(x+1,m) where x = (n + m)/2, otherwise

int f(int n, int m)
{
if ( !(n < m) ) return n == m;
int x = (n + m) / 2
return f(n,x) * f(x+1,m);
}

No comments: