函数
编程求圆周率π的值公式为:π=16*arctan(1/5)-4*arctan(1/239),其中arctan用如下的级数计算:arctan(x)=x-x*x*x/3+x*x*x*x*x/5-x*x*x*x*x*x*x/7+.......,直到级数的某项绝对值不大于10的负十二次方为止。
输入:无。输出:输出结果保留小数点后7位。 #include <stdio.h>
double atanx(double x)
{
double b , e , s ;
int i , j , k ;
for(s = 0 , e = i = k = 1 ; e > 1e-12 ; i += 2 , k = - k) {
for(b = j = 1 ;j <= i ; j ++) b *= x ;
e = b / i ;
s += k * e ;
}
return s ;
}
double pix(void)
{
return 16.0 * atanx(1.0 / 5.0) - 4.0 * atanx(1.0 / 239.0) ;
}
int main(void)
{
printf("%.7lf\n" , pix()) ;
}
编译、运行实况:
D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
3.1415927
D:\00.Excise\C> 本帖最后由 Angelina_Jolie 于 2021-11-3 21:03 编辑
这题太难了
思路就这样
def arctan1(x):
n = 3
while 1:
x1 = -x**n / n
n += 4
def arctan2(x):
n=5
while 1:
x2 = x**n / n
n += 4
def arctan(x):
arctan1(x)
arctan2(x)
if x+x1+x2 > 10**(-12) or((x+x1+x2) * (x+x1+x2) > 10**(-12)):
print(x+x1+x2)
arctan(5)
jackz007 发表于 2021-11-3 20:37
编译、运行实况:
强啊
页:
[1]