pi = 4(1-1/3 + 1/5-1/7 + 1/9-1/11 + 1/13-.........- 1/(2i-1) + 1/(2i+1) )

I am starting this program to approximate pi. Would someone be so kind as to direct me to information concerning this formula?

3
Contributors
5
Replies
7
Views
10 Years
Discussion Span
Last Post by ceyesuma

Yes thanks I found that one earlier.
Here is what I have as it would apply to java.
I have to research further to manipulate the formula.

``````package piproject;
//import java.math.BigDecimal;
/**
*
* @author Administrator
*/
import java.util.*;
public class ComputePi {
public int decimal;
double stop;
int term =0;
double x;//index or decimal places??
double i=10000;
double PI = 0;
double series_neg=(1/(2*i-1));
double series_pos=(1/2*i+1);
double series_one[];//store results of series_neg incrementing i and use as element of result
double series_two[];//store results of series_neg incrementing i and use as element of result
double result_term[];//incorporate in the summation from i=10000 to 100000
public static void main (String[] args){

Scanner reader = new Scanner (System.in);
System.out.println("This program approximates PI .");
//not sure if this is required
System.out.print("Please enter number of decimal places required: ");
//not sure how input is involved;to double stop approximation or to double numDecimalPlaces to indicate percision?
int numDecimalPlaces = reader.nextInt();
System.out.println ("Number of decimal places = " + numDecimalPlaces);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//Problem4.25 Computing pi You can approximate pi by using the following series:
//PI=4(1-1/3+1/5-1/7+1/9-1/11+1/13-...1/(2i-1)+1/(2i+i)
//Write a program that displays the PI value for 9=10000,20000...and 100000.
/////////////////////////////////////////////////////////////////////////////////////////////

public void calcPi(){
//while(stop<100000){
//for(double i;i<100000;i+10000){
//double x=4*(1-(1/(2i-1))+(1/(2i+1)));
}
}``````

Are there other variables that might be needed?

or even less when reading the javadoc for the Math class. It contains the constant Math.PI which is an approximation of Pi (in fact everything is an approximation of Pi as it's impossible to achieve infinite precision which would be required to produce the real value) :)

Yes very true. never the less with approximation being the key word.
I have added a method with a solution to the summation of two terms But I am still working on it. I have to figure out how to make the summation with alternating signs(neg,pos).Has anyone written any code to approximate PI?

``````package piproject;
//import java.math.BigDecimal;
/**
*
* @author Administrator
*/
import java.util.*;

public class ComputePi {
private double j;
public double decimal;
private double stop=100000;
private double index=10000;
private int sign=-1;//must alternate the sign by multiplying by -1??????????
private double term =0;
private double result=0;//   Import java.text.DecimalFormat;???
private double x;//index or decimal places??
private double i;
private double PI = 0;   //   Import java.text.DecimalFormat;???
private double series_neg=(1/(2*i-1));
private double series_pos=(1/2*i+1);
private double series_one[];//store results of series_neg incrementing i and use as element of result_term
private double series_two[];//store results of series_pos incrementing i and use as element of result_term
private double result_term[];//incorporate in the summation from i=10000 to 100000
public static void main (String[] args){

Scanner reader = new Scanner (System.in);
System.out.println("This program approximates PI .");
//not sure if this is required
System.out.print("Please enter number of decimal places required: ");
//not sure how input is involved;to double stop approximation or to double numDecimalPlaces to indicate percision?
int numDecimalPlaces = reader.nextInt();
System.out.println ("Number of decimal places = " + numDecimalPlaces);
}``````
``````/////////////////////////////////////////////////////////////////////////////////////////////////
//Problem4.25 Computing pi You can approximate pi by using the following series:
//PI=4(1-1/3+1/5-1/7+1/9-1/11+1/13-...1/(2i-1)+1/(2i+i)
//Write a program that displays thePI value for 9=10000,20000...and 100000.
//////////////////////////////////////////26
///////////////////////////////////////////////////

public void calcPi(){
for(i=index,j=term;i<stop;i+index;){
result_term[term]=sign*series_neg[i]+series_pos[i];
sign=sign*-1;
result=result_term[term];
}

PI=4*1-result;//not sure of summation of i from 10000 to 100000 and formula
}
}``````

Any related code will be appreciated.

I got the code to approximate PI to 50 decimal places and I was wondering if any one has had experience

``Class BigDecimal``
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.