Results 1 to 11 of 11
  1. #1
    bubbamcgoo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default Helping on Calculating Pie

    Hi everyone, a helping hand will surely help. I am confused with my code. I am trying to solve the question below that solves for pie. When I run the program, the output is:

    The value of pie to the 200,000th term is 4.000000

    This is not the right answer and I can't figure out what I did wrong. Can someone please help? Thanks a lot.


    Question:
    Calculate the value of π from the infinite series
    π = 4 - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + ....
    Print a table that shows the value of π approximated by computing one term of this series, by two terms, by three terms, and so on.



    My Code:
    public class CalculatingPie
    {
    public static void main( String[] args )
    {
    double initial = 4;
    double infiniteSeries = 0;

    for ( int counter = 0; counter <= 100000; counter += 4)
    infiniteSeries = ( 4 / (5 + counter)) - (4 / (3 + counter ));

    double pie = initial + infiniteSeries;

    System.out.printf( "The value of pie to the 200,000th term is %f", pie);

    } // end method main

    } // end class CalculatingPie

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Java Code:
    for ( int counter = 0; counter <= 100000; counter += 4) {
        infiniteSeries = ( 4 / (5 + counter)) - (4 / (3 + counter ));
    }
    double pie = initial + infiniteSeries;
    You only add the last value of infiniteSeries. What about all the other values calculated.

    By the way it is PI. Pie is a pastry filled will meat or fruit.

  3. #3
    bubbamcgoo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    haha.. touche about the pie

    what do you mean that i only add the last value? does the for statement create a loop and causes infiniteSeries to add all the values?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by bubbamcgoo View Post
    causes infiniteSeries to add all the values?
    Please show where in your code it does that. The code below simplies what you are doing.
    Java Code:
    class Test {
        public static void main(String[]args) {
            int value = 1;
            int progress = 0;
            for(int index = 2; index <= 10; index++ ) {
                progress = index;
            }
            int sum = value + progress;
            System.out.println(sum);
        }
    }
    The output is 11 whereas the expectation is that it should output 55.

  5. #5
    bubbamcgoo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    hmmmm.. good point. thanks

    i changed it as follows:


    public class CalculatingPi
    {
    public static void main( String[] args )
    {
    double initial = 4; // initializes pie with 4 and uses for statement to plus/minus the infinite series
    double infiniteSeries = 0;

    for ( int counter = 0; counter <= 100000; counter += 4)
    infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));

    double pi = initial + infiniteSeries;

    System.out.printf( "The value of pie to the 200,000th term is %f", pi);

    } // end method main

    } // end class CalculatingPi



    now i get 3.000000 as the output. still very confused

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    OP how many lines of code is the body of your for loop?

  7. #7
    bubbamcgoo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    hmmmm. not sure what you are asking.

    sorry.. i really am a newbie to this java thing

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    for ( int counter = 0; counter <= 100000; counter += 4)
    infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));
    
    double pi = initial + infiniteSeries;
    is all this code inside your for loop?

  9. #9
    bubbamcgoo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    actually.. the for loop is only

    for ( int counter = 0; counter <= 100000; counter += 4)
    infiniteSeries = infiniteSeries + ( 4 / (5 + counter)) - (4 / (3 + counter ));

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    They were pointing out that you do not have braces around your for loop. A n00b mistake is to think they have 2 or more lines in a loop when they reaaly only have one because they left off the braces.

    Rule of thumb: always use braces around loops and if statements.

  11. #11
    bubbamcgoo is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    thanks. i inserted the braces. but still have the same problem with my output

Similar Threads

  1. need help with calculating something
    By mikec420 in forum New To Java
    Replies: 13
    Last Post: 09-29-2011, 09:14 PM
  2. No one is helping
    By anithajerome in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-06-2010, 12:52 PM
  3. Helping about SWIFT MT Message
    By 82rathi4u in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2009, 01:06 AM
  4. Can someone give me a helping hand...
    By kwesiaryee in forum Advanced Java
    Replies: 1
    Last Post: 09-17-2008, 07:11 PM
  5. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 04:01 PM

Posting Permissions

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