algorithm - How many times function is called to get factorial -
how many times fact(n) called if n 8 , fact(n) recursive implementation finding factorial of number?
i'm assuming program looks like
public static int fact(int n) { if (n == 0 || n == 1) { return 1; } else { return n * fact(n - 1); } } let's see...
- n=8 fact(n) get's called go else and
- with n=7 fact(n) called again go else
- with n=6 fact(n) called
- with n=5
- with n=4
- with n=3
- with n=2
- with n=1
so, here called 8 times.
Comments
Post a Comment