>>  vacuum sealed for your enjoyment
index : pi
I am doing this silly little exercise because Garvin was asking about ways
to improve precision of results when doing this sort of thingy in Java.
After realizing that BigDecimal was well beyond the scope of his class, I
banged out the easy solution. And then I couldn't stop...

--- Java Version ---

public class CalcPI {
	public static void main(String arg[]) {
		double pi = 0.0;
		for( int k = 1; k < Integer.MAX_VALUE/2; k++ ) {
			pi += 4.0 / ((k*2)-1) * (k%2==1?1:-1);
		}
		System.out.println(pi);
	}
}

ammon@hedwig:~/java$ javac CalcPI.java
ammon@hedwig:~/java$ time java CalcPI                                                                
3.1415926526567945

real    0m15.063s
user    0m11.091s
sys     0m0.017s

--- C Version ---

#include <stdio.h>
#define ITERATIONS 1073741824

int main() {
	double pi = 0.0;
	int k;
	for( k = 1; k < ITERATIONS; k++ )
		pi += 4.0 / ((k*2.0)-1) * (k%2?1:-1);
	printf("%0.32f\n",pi); 
	return 0;
}

ammon@hedwig:~$ gcc calcpi.c                                             
ammon@hedwig:~$ time ./a.out 
3.14159265451928115808755137550179

real    0m18.042s
user    0m13.934s
sys     0m0.006s

--- Java BigDecimal Version ---

Really really isn't worth the hassle if you aren't already familiar with how
to use the class. I tried for a few minutes and after getting some code that
I figured should work, it started throwing exceptions because I'm dealing in
nonterminating values (ie, almost every single one of them :P)
©2008 ammon lauritzen