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.H(NoConvergence.java:19)
 *          at NoConvergence.H(NoConvergence.java:20)
 *          at NoConvergence.H(NoConvergence.java:20)
 *          ...
 *************************************************************************/

public class NoConvergence {

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

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

}


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