title: Java Program to determine value of nested radical constant with 10^-6 precision
body: <p>Nested radical constant is defined as:</p>
  
  <p><img src="https://i.stack.imgur.com/rp8T8.jpg" alt="nested radical constant"></p>
  
  <p>I am writing a Java program to calculate the value of nested radical constant with 10^-6 precision and also print the number of iterations required to get to that precision. Here is my code:</p>
  
  <pre><code>public class nested_radical {
  
  public nested_radical() {
      int n = 1;
  
      while ((loop(n) - loop(n - 1)) &gt; 10e-6) {
          n++;
      }
      System.out.println("value of given expression = " + loop(n));
      System.out.println("Iterations required = " + n);
  }
  
  public double loop(int n) {
      double sum = 0;
      while (n &gt; 0) {
          sum = Math.sqrt(sum + n--);
      }
      return (sum);
  }
  
  
  public static void main(String[] args) {
      new nested_radical();
  }
  
  }
  </code></pre>
  
  <p>This code does what it is supposed to but it is slow. What should I do to optimize this program? Can someone suggest another possible way to implement this program?</p>
  
  <p>I also want to write a same kind of program in MATLAB. It would be great if someone could translate this program into MATLAB too.</p>