How do I calculate the Theta runtime of this given code:
void f(int n)
{
for (int i=3; i<n; ++i)
for (int j=0; j<i; ++j)
f(n-1);
}
So far i got this, but I don’t know if it’s right or how to bring it in Theta notation.
f(n) = n^2 * f(n-1)
f(n) = n^2 * (n-1)^2 * f(n-2)
f(n) = n^2 * (n-1)^2 * (n-2)^2 * f(n-3)
...
Since each nested for-loop is O(N^2) complexity, and each time you call a function inside another function, the complexity is multiplied, you end up with O((N!)^2), where
Nis also the number of times you recurse. This is of course becauseN! = N*(N-1)*(N-2)*...*(N-N+1), and all the values used to create the factorial are squared.