1 class Main 3public static void main(String [] args) [ double pi; approximate v
ID: 3755022 • Letter: 1
Question
1 class Main 3public static void main(String [] args) [ double pi; approximate value to he computed int count; final double SYSTEM PI Math.PI; tinal double ONE_ THOUSANDETH -1.E-03; final double ONE_HUNDREDTHOUSANDETH 1.E-05; // Your code goes here System "out , printf("Leibniz after %d iterations : system p: %f approx. p: %f ", count, SYS I EMP, pi); 18 12 13 Instructions from your teacher We learned in a previous assignment that there are many formulas that approximate the value of pi. Pi is an irrational number with an infinite number of digits. There is a representation provided by the Math package (Math.PI) that has this value: 3.14159265359 Let's call this the "system" value. In this assignment you will compute the valuc of pi using the Lcibniz formula using a loop. This formula is known to converge slowly so let's keep track of the number of iterations it takes to come within 1/1000 (one thousandth) of the system value. This is know as the tolerance value. So you want a loop like this computed value 1st term of leibniz formula count1 while (abs (system valuc-computod valuo) >tolerance) adjust computed value by another term count++ end while Here is the Leibniz formula again:Explanation / Answer
Please find Main.java class below:-
1. Main.java
public class Main {
public static void main(String[] args) {
double pi;
int count;
final double SYSTEM_PI = Math.PI;
final double>
final double>
// your code goes here
double acc = 1.0;
count = 0;
int sub = 3;
int sign = -1;
while (Math.abs(4.0 * acc - Math.PI) > ONE_THOUSANDETH) {
acc += sign * (1.0 / sub);
sign *= -1;
sub += 2;
count++;
}
pi = 4.0 * acc;
System.out.printf("Leibnitz after %d iteration: system.pi: %f approx pi: %f", count, SYSTEM_PI, pi);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.