double denom = 3; double pi; double x = -1; int i; double pi2 =0; double denom2 = 0; double pi3 = 0; pi = 4*(1-((double)1/denom)); pi2 = pi; for (i=2; i<=300000 ; i++){ pi3 = pi2; x = x*-1; denom = (denom+2); denom2 = x*((double)(1/denom)); pi2 = pi3 + denom2; if (i%1000 == 0) System.out.println(i + " " + pi2); } } }
this code supposed to calculate this algorithm
4*(1-(1/3)+(1/5)-(1/7)+(1/9).....)
which is equal to pi.. but i dunno if this is right or what... it works but can somebody clarify?