There are many formulas for calculating pi pai in history, in which Gregory and Leibniz found the following formula:
Pai = 4* (1-1/3+1/5-1/7 ...)
The formula is simple and graceful, but in a bad way, it converges too slowly.
If we rounded to keep its two decimal digits, then:
Cumulative 1 items and are: 4.00
Cumulative 2 items and are: 2.67
Cumulative 3 items and are: 3.47
。。。
Please write out what it has accumulated 100 items and how much (rounded to two digits after the decimal).
Source:
#include <stdio.h>
//pai = 4* (1-1/3+1/5-1/7 ...)
Double while ()
{
double sum=0;
int flag=1;//odd Item bit positive, even term is negative
double temp=1;
int i=1;
while (i<=100)
{
temp=1.0/(2*i-1) * (flag);
i++;
Sum=sum+temp;
Flag=-flag;
}
return sum;
}
int main ()
{
double pai=0.0;
Pai=4*while ();
printf ("%.8lf\n", pai);
return 0;
}
Output: 3.13159290, so rounded to 3.13