The Artima Developer Community
Sponsored Link

Java Answers Forum
pi/4 =??

3 replies on 1 page. Most recent reply: Apr 12, 2003 7:22 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Joe

Posts: 5
Nickname: tiger19
Registered: Apr, 2003

pi/4 =?? Posted: Apr 12, 2003 6:19 PM
Reply to this message Reply
Advertisement
I'm really stuck on this program here, hope you guys can help me out:

pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11+...

I'm supposed to evalutate the first 200 terms of this formula and print its approximation of pi.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: pi/4 =?? Posted: Apr 12, 2003 6:47 PM
Reply to this message Reply
/** pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11+...
*/
public class PiCalculator{
    public static void main(String[] args){
        int terms = 200;
        double piEstimate = 0;
        double n = 1;
        for (int i = 0; i < terms; i++){
            if (i%2 == 0) {
                piEstimate = piEstimate + (1 / n); 
            }else{
                piEstimate = piEstimate - (1 / n);                
            }
            n = n + 2;            
        }
        piEstimate = 4 * piEstimate;
        System.out.println("Estimate of pi: " + String.valueOf(piEstimate));
        System.out.println("pi actual: " + String.valueOf(Math.PI));
        System.out.println("pi error: " + String.valueOf(Math.PI - piEstimate));
    }
}

Joe

Posts: 5
Nickname: tiger19
Registered: Apr, 2003

Re: pi/4 =?? Posted: Apr 12, 2003 7:02 PM
Reply to this message Reply
Thanks Charles!

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: pi/4 =?? Posted: Apr 12, 2003 7:22 PM
Reply to this message Reply
Hey, I liked doing that one. I'm a math and physics major from way back.

Flat View: This topic has 3 replies on 1 page
Topic: using timers Previous Topic   Next Topic Topic: Help with code modification

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use