/** This class is our preliminary exploration of recursion.*/
public class Recursion {
  /** Recursively calculate a function t.  t(1) is 2, 
   * t(n|n>1) = 3 * t(n-1) + 1.
   * @param n the value to which the function applies.
   * @return the function evaluated for n.
   */
  public static int t(int n) {
    int result = 0;
    if (n == 1) {
      result = 2;
    } else {
      result =  3 * t(n-1) +1;
    }
    return result;
  }
  
  /** Calculate i factorial recursively.
   * @param i the value for which the factorial is required.
   * @return i factorial as an int.
   */
  public static int factorial(int i) {
    int result = 0;
    if (i == 0 ){ // this is our termination state because any
      result = 1; // recursion terminates here.  All recursion
                  // must have a termination state, and must reach
                  // it through the process or recursion.
    } else {
      result =  factorial(i - 1); // recurse - reduce i and approach
                                  // the termination state.
      /* alternatively, you could infinitely recurse and end up
       * with a stack overflow if you didn't reduce i by 1 and
       * approach the termination state (i.e., when i == 0)
       * You could uncomment the following line, and comment out
       * the previous line to see this happen - try doing this
       * with the debugger to see the stack grow and grow and 
       * grow.
       */
      //    result =  factorial(i); 
                            
      result *= i;
    }
    return result;
  }
  
}