Welcome to Dream.In.Code
Become a Java Expert!

Join 150,362 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,811 people online right now. Registration is fast and FREE... Join Now!




approximating pi

 
Reply to this topicStart new topic

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

ceyesuma
7 Feb, 2008 - 07:42 AM
Post #1

New D.I.C Head
*

Joined: 16 Dec, 2007
Posts: 27


My Contributions
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?
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Approximating Pi
7 Feb, 2008 - 08:29 AM
Post #2

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
QUOTE
pi = 4(1-1/3 + 1/5-1/7 + 1/9-1/11 + 1/13-.........- 1/(2i-1) + 1/(2i+1) )



As I can see it, it contains two AP's as:

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

or

pi=4(Sigma(1/(2i-1))) - 4(Sigma(1/(2i+1)))
with i=0 to n

Choose a large value of n to approximate (Larger the n, more better the approximation.


User is offlineProfile CardPM
+Quote Post

ceyesuma
RE: Approximating Pi
7 Feb, 2008 - 09:07 AM
Post #3

New D.I.C Head
*

Joined: 16 Dec, 2007
Posts: 27


My Contributions
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

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 double 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])+(sign*-1)(series_pos[i]));

sign=sign*-1;
result=result_term[term];
}

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

if someone is farmiliar with a program could someone evauate the calcPi().
Thanks

This post has been edited by ceyesuma: 7 Feb, 2008 - 04:04 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month