Results 1 to 6 of 6
Thread: Gregory Series LIMIT problem
- 08-12-2011, 04:51 PM #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:
and one of the sample outputs should be..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 :)
- 08-12-2011, 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?
- 08-12-2011, 06:11 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
How many terms did your implementation summate?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-12-2011, 09:11 PM #4
Member
- Join Date
- Aug 2011
- Location
- Philippines
- Posts
- 2
- Rep Power
- 0
- 08-12-2011, 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?
- 08-12-2011, 09:30 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Closely read you assignment again:
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.
Originally Posted by assignment
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
alternating series sum java help
By java157 in forum New To JavaReplies: 18Last Post: 03-20-2011, 03:41 AM -
problem to get Fibonacci series- please help.
By zoala001 in forum Java AppletsReplies: 17Last Post: 01-02-2011, 12:50 PM -
Perrin series
By moamen in forum New To JavaReplies: 6Last Post: 12-04-2009, 05:27 PM -
Help with summing series
By xplsivo in forum New To JavaReplies: 8Last Post: 11-23-2009, 07:37 PM -
How to add a second series in jfreechart
By Manfizy in forum New To JavaReplies: 1Last Post: 03-23-2009, 11:16 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks