+ Reply to Thread
Results 1 to 1 of 1

Thread: Calculating Pi

  1. #1
    Join Date
    Jan 2008
    Location
    PA
    Posts
    1,164
    Points
    2,116,615.25
    Rep Power
    209

    Default Calculating Pi

    this was an assignment i had todo today posting it here for no reason.
    pretty much the program uses this weird algorithm.
    http://mathworld.wolfram.com/GregorySeries.html
    simply does pi = 4 * (1 - (1/3) + (1/5) - (1/7).......) etc
    the more times you go through this pattern the more exact you get.
    Code:
    import java.util.Scanner;
    
    public class PiCalc {
        
        public static void main(String[] args) {
        	Scanner keyb = new Scanner(System.in);
        	double pi =0;
        	double numer = 1;
        	double denom = 1;
        	System.out.println("Enter the Number of iterations: ");
        	double counter = keyb.nextDouble();
        	while(counter>0)
        	{
        		pi+= numer/denom;
        		numer*=-1;
        		denom+=2;
        		counter--;
        	}
        	pi*=4.0;
        	System.out.println("Pi Was Calculated As: " + pi);
        }
    }
    ran program with these parameters

    Enter the Number of iterations:
    799999999
    Pi Was Calculated As: 3.1415926548383024

    i googled pi and found it to be
    3.141592653589793238462643383279502884197169399375 10582097494459230781640628620899862803482534211706 79.....
    Last edited by SonniE; 10-19-2009 at 06:27 PM.


    Get Vip: »Here«
    Donate: »Here«
    >>List of Compilers<<
    >>SFDM Name Generator<<
    [Owner Of FluidCoding]

+ Reply to Thread

Posting Permissions

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