Results 1 to 6 of 6
Like Tree4Likes
  • 1 Post By codename
  • 1 Post By Norm
  • 1 Post By JosAH
  • 1 Post By Norm

Thread: Gregory Series LIMIT problem

  1. #1
    codename is offline Member
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    2
    Rep Power
    0

    Question 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);
    
        }
    }
    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 :)
    JeffGrigg likes this.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Have you tried debugging your code by adding printlns to show the variables values as they change while the loop is executing?
    JeffGrigg likes this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    How many terms did your implementation summate?

    kind regards,

    Jos
    JeffGrigg likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    codename is offline Member
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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)..

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    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?
    JeffGrigg likes this.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. alternating series sum java help
    By java157 in forum New To Java
    Replies: 18
    Last Post: 03-20-2011, 03:41 AM
  2. problem to get Fibonacci series- please help.
    By zoala001 in forum Java Applets
    Replies: 17
    Last Post: 01-02-2011, 12:50 PM
  3. Perrin series
    By moamen in forum New To Java
    Replies: 6
    Last Post: 12-04-2009, 05:27 PM
  4. Help with summing series
    By xplsivo in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 07:37 PM
  5. How to add a second series in jfreechart
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 11:16 AM

Posting Permissions

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