Muller.java


Below is the syntax highlighted version of Muller.java from §9.1 Scientific Computation.



/******************************************************************************
 *  Compilation:  javac Muller.java
 *  Execution:    java Muller N
 *
 *  Compute the Muller sequence and try to estimate what it converges to.
 *  It seems to converge to 100, but the correct answer is 5.
 *
 *  % java Muller 5
 *  4.855700712568563
 *  
 *  % java Muller 10
 *  4.987909232795786
 *
 *  % java Muller 20
 *  100.00001247862016
 *
 *  % java Muller 30
 *  100.0
 *
 *  % java Muller 100
 *  100.0
 *
 ******************************************************************************/

public class Muller {

   public static void main(String[] args) { 
      int N = Integer.parseInt(args[0]);
      double[] x = new double[N+1];
      x[0] = 4.0;
      x[1] = 4.25;
      for (int i = 2; i <= N; i++) {
          x[i] = 108.0 - (815.0 - 1500.0 / x[i-2]) / x[i-1];
       }
       StdOut.println(x[N]);
   }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.