# Thread: Gregory Series LIMIT problem

1. Member Join Date
Aug 2011
Location
Philippines
Posts
2
Rep Power
0

## 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:

Java 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
//   term from the running sum...
double sum = 0;
int term = 1;

while (4 / divisor >= limit) {

pi += 4 / divisor;
} else {
pi -= 4 / divisor;
}

// update variables...
divisor += 2;
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 :)  Reply With Quote

2. ## Have you tried debugging your code by adding printlns to show the variables values as they change while the loop is executing?  Reply With Quote

3. ## How many terms did your implementation summate?

kind regards,

Jos  Reply With Quote

4. Member Join Date
Aug 2011
Location
Philippines
Posts
2
Rep Power
0

##  Originally Posted by JosAH How many terms did your implementation summate?

kind regards,

Jos
the terms were the same with the sample output, however the calculated pi is somewhat different (decimal values)..  Reply With Quote

5. ## Let me ask JosAH's question another way:
If you add a println inside the loop and counted the number of lines printed, would that be the number of terms that were added?  Reply With Quote

6. ##  Originally Posted by assignment
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:
Your algorithm should add (or subtract) one term less than the limit and then quit the iteration. Your implementation doesn't do that, i.e. it quits one step before that.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•