5 Responses

  1. Brian Gbaguidi
    Brian Gbaguidi 27/11/2012 at 10:01 | | Reply

    The factorial function should be:
    public int factorial(int n){
    if(n == 1){
    return 1;
    }else{
    return n * factorial(n – 1);
    }
    }

    return 0 if n == 1 make the factorial function return 0 for each n.

  2. Brian Gbaguidi
    Brian Gbaguidi 27/11/2012 at 10:36 | | Reply

    if you want to save the extra step in your algorithm you should have wrote “if(n==1) return 1;”
    Otherwise you would have wrote “if(n==0) return 1;” Which is the formal definition.
    But if(n==1) return 0; doesn’t work. It makes your function return 0 for whatever n.

  3. The Beauty of Recursion 30/01/2013 at 08:46 |

Leave a Reply


three + two =

* Copy This Password *

* Type Or Paste Password Here *