Results 1 to 13 of 13
  1. #1
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default help w/ PI Math java program

    im currently in entry level java class and need help with a code..here are my instructions

    Mathematicians define the constant PI to be
    PI = 4 *( 1 - 1/(2*2-1) + 1/(2*2+1) - 1/(2*4-1) + 1/(2*4+1) - 1/(2*6-1) + 1/(2*6+1)- . . . .format . . . . . - 1/(2*i-1) + 1/(2*i+1) )
    Note that i starts at 2 and is incremented by 2. The higher the i, the more precise PI will be. Also note that the higher the value of i, the smaller the value of PI.

    Write a program that displays the value of PI for i = 100, 200, 300, . . ,1000. Output should be something like this :
    when i is 100 PI is xxxx
    when i is 200 PI is xxxx
    etc . .
    when i is 900 PI is xxxx
    when i is 1000 PI is xxxx

    Once you have this working, enhance the program, add another section. The next section of the program should determine the value of i needed to achieve a value of PI that is equal to or slightly smaller than 3.1419. The program should output the following message :
    value of i to give PI equal or smaller than 3.1419 is xxxxxx


    and here is my code, i underlined the part where im getting an errors
    should i put each value of PI in an if statement?? im kinda stuck right now thanks

    Java Code:
    public class ConstantPI {
    public static void main(String[] args)
    {
    	
    
    [U]int i = 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000;[/U]
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    double pi = 4.0*pi4;
    System.out.println("When i is " + i + " PI is " + pi);
    }
    
    }
    btw cross posted thanks
    http://www.javaprogrammingforums.com...html#post26927
    Last edited by clemsontigers; 02-16-2011 at 03:31 AM.

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

    Default

    Java Code:
    int i = 100
    That assigns 100 to i. What do you expect the rest of the line of code to do?

  3. #3
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    alright here is my updated code but still not sure how to calculate the pi when i =200,300, etc

    also i tried to work on the 2nd part of my code

    Java Code:
    public class Assign3_Roberts {
    public static void main(String[] args)
    {
    	
    
    int i = 100;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    double pi = 4.0*pi4;
    System.out.println("When i is " + i + " PI is " + pi);
    }
    
    
    
    //next section
    //value of i
    int i = 0;
    double pi = 100;
    {
    do
    {
    i ++;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    pi = 4.0*pi4;
    } while (pi > 3.1419);
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    }
    }
    and when i run the program my output is:

    When i is 100 PI is 3.1514934010709914

  4. #4
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    alright i updated my code w/ a for loop and believe im getting some where

    Java Code:
    public class Assign3_Roberts {
    public static void main(String[] args)
    {
    for (int i = 100; i <= 3300; i += 100) {
    	double pi4 = 1.0;
    	for(int j = 2; j <= i; j += 2)
    	pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    	double pi = 4.0*pi4;
    	System.out.println("When i is " + i + " PI is " + pi);
    	}
    	}
    
    //next section
    //determine value of i
    
    int i = 0;
    double pi = 100;
    {
    do
    {
    i ++;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    pi = 4.0*pi4;
    } while (pi > 3.1419);
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    }
    }
    and output is now:
    When i is 100 PI is 3.1514934010709914
    When i is 200 PI is 3.1465677471829556
    When i is 300 PI is 3.1449149035588526
    When i is 400 PI is 3.144086415298761
    When i is 500 PI is 3.143588659585789
    When i is 600 PI is 3.143256545948974
    When i is 700 PI is 3.1430191863875865
    When i is 800 PI is 3.142841092554028
    When i is 900 PI is 3.1427025311614294
    When i is 1000 PI is 3.1425916543395442

    but not getting the 2nd section right
    Last edited by clemsontigers; 02-16-2011 at 07:18 PM.

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

    Default

    Quote Originally Posted by clemsontigers View Post
    and output is now:
    When i is 100 PI is 3.1514934010709914
    When i is 200 PI is 3.1465677471829556
    When i is 300 PI is 3.1449149035588526
    When i is 400 PI is 3.144086415298761
    When i is 500 PI is 3.143588659585789
    When i is 600 PI is 3.143256545948974
    When i is 700 PI is 3.1430191863875865
    When i is 800 PI is 3.142841092554028
    When i is 900 PI is 3.1427025311614294
    When i is 1000 PI is 3.1425916543395442

    but not getting the 2nd section right
    So the maximum value of i should go up; as you can see from your output the series slowly goes down (as your assignment text already told you). The series has a very slow convergence so you should add more terms to reach a value lower than 3.1419.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    ok well i changed it to 3300 and updated it in my previous post and here is my output

    When i is 100 PI is 3.1514934010709914
    When i is 200 PI is 3.1465677471829556
    When i is 300 PI is 3.1449149035588526
    When i is 400 PI is 3.144086415298761
    When i is 500 PI is 3.143588659585789
    When i is 600 PI is 3.143256545948974
    When i is 700 PI is 3.1430191863875865
    When i is 800 PI is 3.142841092554028
    When i is 900 PI is 3.1427025311614294
    When i is 1000 PI is 3.1425916543395442
    When i is 1100 PI is 3.14250091861592
    When i is 1200 PI is 3.142425292912591
    When i is 1300 PI is 3.1423612929843348
    When i is 1400 PI is 3.1423064293732548
    When i is 1500 PI is 3.142258876034187
    When i is 1600 PI is 3.1422172631478604
    When i is 1700 PI is 3.1421805430157748
    When i is 1800 PI is 3.1421479006319486
    When i is 1900 PI is 3.14211869248028
    When i is 2000 PI is 3.1420924036835256
    When i is 2100 PI is 3.142068617389586
    When i is 2200 PI is 3.1420469925031944
    When i is 2300 PI is 3.1420272472242043
    When i is 2400 PI is 3.1420091466995945
    When i is 2500 PI is 3.141992493637787
    When i is 2600 PI is 3.141977121088081
    When i is 2700 PI is 3.1419628868240537
    When i is 2800 PI is 3.1419496689300788
    When i is 2900 PI is 3.1419373623006863
    When i is 3000 PI is 3.1419258758397897
    When i is 3100 PI is 3.141915130201854
    When i is 3200 PI is 3.14190505595643
    When i is 3300 PI is 3.1418955920863283

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

    Default

    Quote Originally Posted by clemsontigers View Post
    ok well i changed it to 3300 and updated it in my previous post and here is my output

    ...
    When i is 3300 PI is 3.1418955920863283
    Are you sure the test fails for i == 3298?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    i believe i only go up in increments of 100 b/c when i entered 3298 it only gave me the output of 100-3200

  9. #9
    clemsontigers is offline Member
    Join Date
    Oct 2010
    Posts
    44
    Rep Power
    0

    Default

    it never showed the output of what the value of I is for PI to = 3.1419 or lower

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by clemsontigers View Post
    it never showed the output of what the value of I is for PI to = 3.1419 or lower

    Java Code:
    while (pi > 3.1419)[COLOR="Red"];[/COLOR]
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    What do you think the semicolon after the while loop is doing?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    javaforum$ is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default There's a mismatch in '{' and '}' to print the statement "value of i to give PI .."

    public static void main(String[] args)
    {
    for (int i = 100; i <= 3300; i += 100) {
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    double pi = 4.0*pi4;
    System.out.println("When i is " + i + " PI is " + pi);
    }
    } < -- Remove this

    //next section
    //determine value of i

    int i = 0;
    double pi = 100;
    { < -- Remove this
    do
    {
    i ++;
    double pi4 = 1.0;
    for(int j = 2; j <= i; j += 2)
    pi4 = pi4 - 1/(2*(double)j - 1) + 1/(2*(double)j + 1);
    pi = 4.0*pi4;
    } while (pi > 3.1419);
    System.out.println("value of i to give PI equal or smaller than 3.1419 is " + i);
    } < -- Remove this
    }

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Follow javaforum$ advice.

    I kinda misread that as a start of while loop, rather than end of do-while loop. Sorry, but I need some coffee :(

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Quote Originally Posted by clemsontigers View Post
    i believe i only go up in increments of 100 b/c when i entered 3298 it only gave me the output of 100-3200
    That's what the 'enhancemant' your assignment described is all about. You should add terms (and increment i) as long as your approximation of PI is larger than that value. Other replies already showed you how it can be done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with java math
    By gabrielfirestorm in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 01:54 PM
  2. Help need on math java program
    By zidangus in forum New To Java
    Replies: 27
    Last Post: 07-07-2010, 02:48 AM
  3. Math Program
    By stlboi in forum New To Java
    Replies: 8
    Last Post: 04-02-2009, 09:34 PM
  4. need help with math for a new program
    By gotenks05 in forum New To Java
    Replies: 13
    Last Post: 09-26-2008, 07:32 AM
  5. Java Math
    By levent in forum Java Tutorial
    Replies: 1
    Last Post: 05-12-2008, 09:03 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
  •