Results 1 to 6 of 6
Thread: Gregory Series LIMIT problem
 08122011, 04:51 PM #1Member
 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 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); } }
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 :)
 08122011, 05:25 PM #2
Have you tried debugging your code by adding printlns to show the variables values as they change while the loop is executing?
 08122011, 06:11 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
How many terms did your implementation summate?
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 08122011, 09:11 PM #4Member
 Join Date
 Aug 2011
 Location
 Philippines
 Posts
 2
 Rep Power
 0
 08122011, 09:20 PM #5
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?
 08122011, 09:30 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
Closely read you assignment again:
Originally Posted by assignment
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

alternating series sum java help
By java157 in forum New To JavaReplies: 18Last Post: 03202011, 04:41 AM 
problem to get Fibonacci series please help.
By zoala001 in forum Java AppletsReplies: 17Last Post: 01022011, 01:50 PM 
Perrin series
By moamen in forum New To JavaReplies: 6Last Post: 12042009, 06:27 PM 
Help with summing series
By xplsivo in forum New To JavaReplies: 8Last Post: 11232009, 08:37 PM 
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03232009, 12:16 PM
Bookmarks