help... is this right? need some1 to clarify...

Code java:

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? :(

Re: help... is this right? need some1 to clarify...

Quote:

it works but can somebody clarify? :(

Please describe what clarification is needed. If it works and you don't understand...when all else fails, grab a pen and pencil and walk through the algorithm bit by bit (maybe not 30,000 times, but perhaps lower that loop value and println the output every loop, and check against the pen and pencil).

Re: help... is this right? need some1 to clarify...

it works my calculation works fine but when i run the program it doesnt come close to the value of pi which is 3.14 sumthing only up to 2.7 sumthing..

am i doing the calculation wrong?

i did... 4*(1-(1/3)) 1st then the result will be either added or subracted by an increasing denom by 2.....

is that the proper way?