Thank you for this question. I had seen these formulas but not
implemented them before. It was interesting to see how quickly the
Vieta formula converged.
By the way, should you have any questions about the answer or need
clarification or new features, please use the "Request Clarification"
button on the browser to solicit it.
I created an abstract base class named Pythagoras with two concrete
subclasses, Wallis and Vieta. An instance of a class holds a number of
Terms and a value.
The base class has an abstract method compute() which is overridden in
each subclass in order to perform the computation.
There is also a testing class Pi . When run, it prompts the user for
the number of terms, runs each computation using that number of terms,
and prints the result.
Unfortunately, in Java to read input from the terminal is a little bit
messy. That is why I had to do all that stuff with BufferedReader and
so on in the class Pi. This is standard boilerplate that is just
copied from program to program.
If you enjoy mathematical computations, or just want to learn Java
better, I recommned trying out the site http://www.topcoder.com . It's
pretty fun. There is a contest there tonight in fact.
Below I am including a sample script below. You can download the four
files from:
http://www.rbnn.com/google/106752/Pi.java
http://www.rbnn.com/google/106752/Pythagoras.java
http://www.rbnn.com/google/106752/Vieta.java
http://www.rbnn.com/google/106752/Wallis.java
To compile them, just put them into their own directory and do
javac Pi.java
To run, use
java Pi
% javac Pi.java
% java Pi
Input the desired number of terms (-1 to exit): 100
Running Vieta for: 100 terms
Got Vieta value of: 3.141592653589794 for an error of:
8.881784197001252E-16
Running Wallis for: 100 terms
Got Wallis value of: 3.126078900215409 for an error of:
-0.015513753374384098
Input the desired number of terms (-1 to exit): 10000
Running Vieta for: 10000 terms
Got Vieta value of: 3.141592653589794 for an error of:
8.881784197001252E-16
Running Wallis for: 10000 terms
Got Wallis value of: 3.1414355935898644 for an error of:
-1.5705999992876585E-4
Input the desired number of terms (-1 to exit): 20000
Running Vieta for: 20000 terms
Got Vieta value of: 3.141592653589794 for an error of:
8.881784197001252E-16
Running Wallis for: 20000 terms
Got Wallis value of: 3.141514118681855 for an error of:
-7.853490793818807E-5
Input the desired number of terms (-1 to exit): -1
Although I recommend downloading the files from the above site, for
completeness here are the files:
public abstract class Pythagoras{
public int numberTerms;
protected double value;
public Pythagoras(int terms){
numberTerms=terms;
}
public double getValue(){return value;}
public String toString(){return "Pythagoras: "+value;}
public double error(){return value-Math.PI;}
public abstract double compute();
}
public class Pi{
static java.io.BufferedReader reader;
static Vieta vieta;
static Wallis wallis;
public static void main(String[] args){
reader=
new java.io.BufferedReader
(new java.io.InputStreamReader
(System.in));
while(true){
System.out.print("Input the desired number of terms (-1 to exit):
");
String line=null;
try{
line=reader.readLine();}
catch(Exception e){
System.exit(1);
}
int nterms=Integer.parseInt(line);
if (nterms<1)
System.exit(0);
vieta=new Vieta(nterms);
System.out.println("\nRunning Vieta for: "+nterms+" terms");
double vvalue=vieta.compute();
double verror=vieta.error();
System.out.println("Got Vieta value of: "+vvalue+" for an error
of: "+verror);
wallis=new Wallis(nterms);
System.out.println("\nRunning Wallis for: "+nterms+" terms");
double wvalue=wallis.compute();
double werror=wallis.error();
System.out.println("Got Wallis value of: "+wvalue+" for an error
of: "+werror);
}
}
}
/* Using formula from Vietas:
2 sqrt(2) sqrt(2+sqrt(2)) sqrt(sqrt(2+sqrt(2)))
sqrt(sqrt(sqrt(2+sqrt(2))))
---- = --------- X ----------------- X -----------------------
X----------------------------- X ....
PI 2 2 2
*/
public class Vieta extends Pythagoras{
public Vieta(int terms){
super(terms);
}
public double compute(){
double numerator=0;
value=0;
if (numberTerms<=0)return value;
double product=1;
for (int i=0;i<numberTerms;++i){
numerator=Math.sqrt(2+numerator);
product*=numerator/2;
}
return value=1/(product/2);
}
}
/*
Wallis formula
PI 2 2 4 4 6 6 8 8 10 10
---- = --- X --- X --- X --- X --- X --- X --- X --- X ---- X ---- X
....
2 1 3 3 5 5 7 7 9 9 11
*/
public class Wallis extends Pythagoras{
public Wallis(int terms){
super(terms);
}
public double compute(){
value=0;
if (numberTerms<=0)return value;
double product=1;
double numerator=0;
double denominator;
for (int i=0;i<numberTerms;++i){
denominator=numerator+1;
if (i%2==0)numerator+=2;
product*=numerator/denominator;
}
return value= product*2;
}
} |