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...

  1. n=8 fact(n) get's called go else and
  2. with n=7 fact(n) called again go else
  3. with n=6 fact(n) called
  4. with n=5
  5. with n=4
  6. with n=3
  7. with n=2
  8. with n=1

so, here called 8 times.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -