I need to estimate PI by doing following series:
m(i) = 4( 1- 1/3 + 1/5 - 1/7 + 1/9 - 1/11 ...)
And this is what I've done so far:
float mOfI = 1;
System.out.println("i \t \t \t \t m(i)" );
for (int i = 1; i < n; i++) {
float sum = i + 2;
mOfI += 4 * (1 - (1 / sum));
mOfI -= 4 * (1 - (1 / sum));
System.out.println(i + "\t \t \t \t" +mOfI);
}
I know I'm missing many rules here, but how can I make it to work correctly? I mean the math logic. How can I can solve it correctly?
Please Note the -+
in the series and all numbers are odd so i can't use i%2
.
Estimated PI would be like 4.0000 , 3.1515, 3.1466 .....
and so on.
Also this question didn't received a well answer (there is no real PI value estimated)
Thanks all for your time, but i got the right solution:
double start = 1; // Start series
double end = 901; // End series
System.out.println("\ni m(i) ");
System.out.println("---------------------");
for (double i = start; i <= end; i += 100) {
System.out.printf("%-12.0f", i);
System.out.printf("%-6.4f\n", estimatePI(i));
}
}
/** Method estimatePI */
public static double estimatePI(double n) {
double pi = 0; // Set pi to 0
for (double i = 1; i <= n; i ++) {
pi += Math.pow(-1, i +1) / (2 * i - 1);
}
pi *= 4;
return pi;
}