GoldenRatio.java


Below is the syntax highlighted version of GoldenRatio.java from §2.3 Recursion.


/*************************************************************************
 *  Compilation:  javac GoldenRatio.java
 *  Execution:    java GoldenRatio N
 *  
 *  Computes an approximation to the golden ratio using the recursive
 *  formula f(0) = 1, f(n) = 1 + 1 / f(n-1) if n > 0.
 *
 *  % java GoldenRatio 5
 *  1.625
 *
 *  % java GoldenRatio 10
 *  1.6179775280898876
 *
 *  % java GoldenRatio 20
 *  1.618033985017358
 *
 *  % java GoldenRatio 30
 *  1.6180339887496482
 *
 *************************************************************************/

class GoldenRatio {
   public static double golden(int n) {
       if (n == 0) return 1;
       return 1.0 + 1.0 / golden(n-1);
   }

   public static void main(String[] args) {
      int N = Integer.parseInt(args[0]);
      System.out.println(golden(N));
   }

}



Copyright © 2000–2010, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:05:37 EST 2011.