Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program/class utilizing a recursive method to estimate the value of

ID: 3787645 • Letter: W

Question

Write a Java program/class utilizing a recursive method to estimate the value of PI using the Gregory-Leibniz series:

= (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) ...

Determine the number of iterations (terms) necessary to calculate PI to within .00000001



I have already done the recursive method;

//recursive method
public static double LeibnizSeries(int num){
if (num ==0){
return (4.0/1.0);
} else {
return (Math.pow(-1,num) * (4.0/(2.0*num + 1.0))) + LeibnizSeries(num-1);
}
}

I just dont know how to use it to get the number of terms to calculate PI to within .00000001.

Explanation / Answer

The number of iterations would be simply:

4/n = 0.00000001
n = 4/(0.00000001)
n = 4*10^8
But it will create a stack overflow error to achieve this.
Here is the code :

public class cal_pi {
   public static double LeibnizSeries(int num)
   {
       if (num ==0)
       {
return (4.0/1.0) + LeibnizSeries(num+1);
}
       double d = (4.0/(2.0*num + 1.0));
       System.out.println(num + " "+d);
       if(d<=0.00000001)
       {
           return 0;
       }
       if(num%2==1)
       {
           d = d*-1.0;
       }
return d+LeibnizSeries(num+1);   
   }
   public static void main(String[] args)
   {
       System.out.println(LeibnizSeries(0));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote