NoConvergence.java


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


/******************************************************************************
 *  Compilation:  javac NoConvergence.java
 *  Execution:    java NoConvergence n
 *
 *  A recursive function whose reduction step does not converge to the
 *  base case.
 *
 *  % java NoConvergence 5
 *  Exception in thread "main" java.lang.StackOverflowError
 *          at NoConvergence.harmonic(NoConvergence.java:19)
 *          at NoConvergence.harmonic(NoConvergence.java:20)
 *          at NoConvergence.harmonic(NoConvergence.java:20)
 *          ...
 ******************************************************************************/

public class NoConvergence {

    public static double harmonic(int n) {
        if (n == 1) return 1.0;
        return harmonic(n) + 1.0/n;
    }

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        StdOut.println(harmonic(n));
    }

}


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