Gregory Series LIMIT problem
hi guys, i'm having trouble with my java homework. it says that.. The program shall proceed to calculate the value of pi by summing up the terms of the series, but only until such time that the value of the term being summed becomes less than or equal to the value of limit, at which point the program terminates the summation. Thus, the last term of the series that is added to the sum is the first term whose value is less than or equal to the value of limit. and this is my code:
Code:
import java.lang.*;
import java.util.Scanner;
import static java.lang.System.out;
public class GregorySeriesLimit {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.print("Input Limit: ");
double limit = kbd.nextDouble();
// initialize variables...
double pi = 0; // 'running' sum...
double divisor = 1; // divisor for the current term
boolean add = true; // flag whether to add or subtract
// term from the running sum...
double sum = 0;
int term = 1;
while (4 / divisor >= limit) {
if (add) {
pi += 4 / divisor;
} else {
pi -= 4 / divisor;
}
// update variables...
divisor += 2;
add = !add;
term++;
}
out.println("Calculated Value of PI: " + pi);
out.println("No. of terms summed: " + term);
}
}
and one of the sample outputs should be..
Input Limit: 0.075
Calculated Value of PI: 3.1058897382719475
No. of Terms Summed: 28
but whenever i input 0.075 as my limit, the calculated value of pi is 3.1786170109992202 ...
can someone help me in my code about the pi? i would gladly appreciate it.. thx :)