Results 1 to 5 of 5
  1. #1
    kissmeeq is offline Member
    Join Date
    Aug 2011
    Posts
    8
    Rep Power
    0

    Default i have a problem

    this is my problem:


    Problem 1: (GregorySeriesLimit.java)
    The number “pi” (3.14159...) can not be expressed as a simple ratio of two numbers. Instead, the value of pi is typically calculated by summing up the terms of an infinite number series. As more and more terms in the series are evaluated, the sum approaches the “true” value of pi. One series that can be used for this purpose is called the Gregory series, which computes the value of pi as follows:
    pi = 4/1 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …
    Observe that the numeric values of the terms of the series get smaller and smaller as the calculation progresses. For example, the value of the 1st term is 4/1 = 4, the 2nd term is 4/3 = 1.333…, the 3rd term is 4/5 = 0.8, and so on.
    Write a program that calculates the value of pi using the Gregory series. The input to the program will be a decimal value called limit. 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.
    The program then prints out the calculated value of pi at that point, as well asthe actual number of terms that were summed up by the calculation.


    and this must be the output:


    Input Limit: 0.005

    Calculated Value of PI: 3.1659792728432157
    No. of Terms Summed: 41


    but the output of my program is wrong
    here it is:

    Input limit: 0.005
    Calculated Value of pi: 4.0
    Number of terms used: 2



    AND HERE IS MY CODE:


    import java.util.Scanner;



    public class GregorySeries {



    public static void main(String[] args) {

    Scanner kbd=new Scanner(System.in);

    System.out.print("Input limit: ");

    double limit = kbd.nextDouble(); //value of the limit

    int term = 1; //number of terms

    double pi = 0; //value of pi

    double divisor = 1; //divisor

    boolean add =true; //boolean statement for getting pi



    while (pi <= limit) { //Im not sure on what should be my loop statement



    if (add) {

    pi += 4 / divisor; //loop to add

    } else {

    pi -= 4 / divisor; //loop to subtract

    }



    divisor += 2; // add two to divisor

    add = !add; //change to subtract>add>subtract.....

    term++; //add one to term





    }

    System.out.println("Calculated Value of pi: " + pi);

    System.out.println("Number of terms used: " + term);

    }

    }

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

    Default

    Try debugging your code by Adding some printlns to your code to show how the values of variables are changing.

  3. #3
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by kissmeeq View Post
    this is my problem:


    while (pi <= limit) { //Im not sure on what should be my loop statement

    }
    It should not be pi<=limit
    it should be 4/divisor>=limit

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    only until such time that the value of the term being summed becomes less than or equal to the value of limit,
    4/divisor > limit

  5. #5
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    In your case, it will enter the loop ONLY when pi<=limit (which happens for the 1st time you enter which is because pi is initialized to zero) then the next loop it finds pi=4 (the first term) and term =2.. So, it terminates.
    One more thing, term should be initialized to 0 not 1..

Posting Permissions

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