factorial


See On Github

Data

Source Code

/*
 * @author Jared Halpert (ghostsnstuff)
 * @date 2/8/14
 * @complexity O(n)
 * @logic
 *  execution happens once every time the value of n is decremented
 *  the value of n is decremented until it reaches 1
 *  therefore the function is called recursively n times.
 */

public class Factorial {
  
  public int computeFactorial(int n) {
    if(n <= 1) {
      return 1;
    } else {
      return computeFactorial(n-1) * n;
    }
  }

}
public class Factorial_Test {
  
  public static void main(String[] args) {
    System.out.println("The anticipated output for 5! = 120"); 
    Factorial factorial = new Factorial();
    int result = factorial.computeFactorial(5);
    System.out.print("factorial.computeFactorial(5) = ");
    System.out.println(result);
  }

}