Answered # approximating pi

Ezzaral 2,714 Discussion Starter ceyesuma -4 jwenting 1,594 Discussion Starter ceyesuma -4 Discussion Starter ceyesuma -4

0

Well, it took about 2 seconds to find with Google:

http://en.wikipedia.org/wiki/Pi#Calculating_.CF.80

0

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?

0

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) :)

0

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.

0

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.

Recommended Articles

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`