# Gregory Series LIMIT problem

• 08-12-2011, 04:51 PM
codename
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 :)
• 08-12-2011, 05:25 PM
Norm
Have you tried debugging your code by adding printlns to show the variables values as they change while the loop is executing?
• 08-12-2011, 06:11 PM
JosAH
How many terms did your implementation summate?

kind regards,

Jos
• 08-12-2011, 09:11 PM
codename
Quote:

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)..
• 08-12-2011, 09:20 PM
Norm
Let me ask JosAH's question another way:
How many terms are added together in your program?
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?
• 08-12-2011, 09:30 PM
JosAH
Closely read you assignment again:

Quote:

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