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
 *
 ******************************************************************************/

public 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]);
        StdOut.println(golden(n));
    }

}



Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:19:56 EDT 2022.