python代码:
def pi(n): sum = 0.0 for i in range(n): sign = (-1) ** i term = 1 / (i * 2 + 1) sum += sign * term return 4.0 * sum n = 10000 # 设置迭代次数 print("π的近似值为:", pi(n)) print("4347")
运行结果:
标签:term,return,sum,sign,计算,print,pi From: https://www.cnblogs.com/LXxx007/p/17801790.html