C语言循环实现数学公式求圆周率π

1、韦达公式

第一种形式
2 π = 2 2 ⋅ 2 + 2 2 ⋅ 2 + 2 + 2 2 ⋯ {\Large {\color{Black} \frac{2}{\pi}=\frac{\sqrt{2}}{2} \cdot \frac{\sqrt{2+\sqrt{2}}}{2} \cdot \frac{\sqrt{2+\sqrt{2+\sqrt{2}}}}{2} \cdots } } π2=22 22+2 22+2+2

第二种形式:取倒数
π 2 = 2 2 ⋅ 2 2 + 2 ⋅ 2 2 + 2 + 2 ⋯ {\Large {\color{Black} \frac{\pi}{2}=\frac{2}{\sqrt{2}} \cdot \frac{2}{\sqrt{2+\sqrt{2}}} \cdot \frac{2}{\sqrt{2+\sqrt{2+\sqrt{2}}}} \cdots}} 2π=2 22+2 22+2+2 2

第三种形式:分子分母都在根号里
2 π = 1 2 ⋅ 1 2 + 1 2 1 2 ⋅ 1 2 + 1 2 1 2 + 1 2 1 2 ⋯ {\Large{\color{Black} \frac{2}{\pi}=\sqrt{\frac{1}{2}} \cdot \sqrt{\frac{1}{2}+\frac{1}{2} \sqrt{\frac{1}{2}}} \cdot \sqrt{\frac{1}{2}+\frac{1}{2} \sqrt{\frac{1}{2}+\frac{1}{2} \sqrt{\frac{1}{2}}}} \cdots}} π2=21 21+2121 21+2121+2121





(1)第一种形式
2 π = 2 2 ⋅ 2 + 2 2 ⋅ 2 + 2 + 2 2 ⋯ {\Large {\color{Black} \frac{2}{\pi}=\frac{\sqrt{2}}{2} \cdot \frac{\sqrt{2+{\color{Red} \sqrt{2}} }}{2} \cdot \frac{\sqrt{2+{\color{Red} \sqrt{2+\sqrt{2}} }}}{2} \cdots}} π2=22 22+2 22+2+2

将分子单独作为一个数列,得出递推公式

2 2 + 2 2 + 2 + 2 ⋯ 递推公式: a n = 2 + a n − 1 {\Large {\color{Black} \begin{align*} & \sqrt{2}\quad \sqrt{2+{\color{Red} \sqrt{2}}}\quad \sqrt{2+{\color{Red} \sqrt{2+\sqrt{2}} } } \cdots \\ \\ &递推公式:a_{n}=\sqrt{2+a_{n-1}} \end{align*} }} 2 2+2 2+2+2 递推公式:an=2+an1

得到分子的递推公式:n=sqrt(2+n),那么分式整体就是n/2

等式右侧是连乘,所以用 *= 进行累乘,用prod保存乘积,迭代100次

因为π在分母位置,所以用π=2/乘积prod来计算

#include<stdio.h>
#include<math.h>
int main()
{
	double n=0,prod=1,pi;
	int i;
	for(i=1; i<=100; i++)
	{
		n=sqrt(2+n);
		prod*=n/2;
	}
	pi=2/prod;
	printf("%f", pi);
	return 0;
}



(2)第二种形式只是变成倒数

π 2 = 2 2 ⋅ 2 2 + 2 ⋅ 2 2 + 2 + 2 ⋯ {\Large {\color{Black} \frac{\pi}{2}=\frac{2}{\sqrt{2}} \cdot \frac{2}{\sqrt{2+{\color{Red} \sqrt{2}} }} \cdot \frac{2}{\sqrt{2+{\color{Red} \sqrt{2+\sqrt{2}} }}} \cdots}} 2π=2 22+2 22+2+2 2

#include<stdio.h>
#include<math.h>
int main()
{
	double n=0,prod=1,pi;
	int i;
	for(i=1; i<=100; i++)
	{
		n=sqrt(2+n);
		prod*=2/n;
	}
	pi=2*prod;
	printf("%f", pi);
	return 0;
}



(3)第三种形式套路相同,但是分子分母都在根号里,所以递推公式有所改变
2 π = 1 2 ⋅ 1 2 + 1 2 1 2 ⋅ 1 2 + 1 2 1 2 + 1 2 1 2 ⋯ 递推公式: a n = 1 2 + 1 2 a n − 1 {\Large {\color{Black} \begin{align*} &{\Large \frac{2}{\pi}=\sqrt{\frac{1}{2}} \cdot \sqrt{\frac{1}{2}+ \frac{1}{2} {\color{Red}\sqrt{\frac{1}{2}} }} \cdot \sqrt{\frac{1}{2}+ \frac{1}{2} {\color{Red}\sqrt{\frac{1}{2}+\frac{1}{2} \sqrt{\frac{1}{2}}} }} \cdots}\\ \\ &递推公式:a_{n}=\sqrt{\frac{1}{2} +\frac{1}{2}a_{n-1}} \end{align*} }} π2=21 21+2121 21+2121+2121 递推公式:an=21+21an1

用0.5表示二分之一,得到整个分数的递推公式:n=sqrt(0.5+0.5*n);

#include<stdio.h>
#include<math.h>
int main()
{
	double n=0,prod=1,pi;
	int i;
	for(i=1; i<=100; i++)
	{
		n=sqrt(0.5+0.5*n);
		prod*=n;
	}
	pi=2/prod;
	printf("%f", pi);
	return 0;
}






2、沃利斯公式


(1)项数为偶数个,按以下规律两两组合计算

π 2 = 2 1 ⋅ 2 3 ⋅ 4 3 ⋅ 4 5 ⋅ 6 5 ⋅ 6 7 ⋅ 8 7 ⋅ 8 9 ⋯ = ∏ n = 1 ∞ 2 n 2 n − 1 ⋅ 2 n 2 n + 1 = ∏ n = 1 ∞ 4 n 2 4 n 2 − 1 {\Large {\color{Black} \begin{align*} \frac{\pi}{2} &=\frac{2}{1} \cdot \frac{2}{3} \cdot \frac{4}{3} \cdot \frac{4}{5} \cdot \frac{6}{5} \cdot \frac{6}{7} \cdot \frac{8}{7} \cdot \frac{8}{9} \cdots \\[2ex] &=\prod_{n=1}^{\infty} \frac{2 n}{2 n-1} \cdot \frac{2 n}{2 n+1}\\[2ex] &=\prod_{n=1}^{\infty} \frac{4 n^{2}}{4 n^{2}-1} \end{align*} }} 2π=1232345456767898=n=12n12n2n+12n=n=14n214n2

#include<stdio.h>
#include<math.h>
int main()
{
	double prod=1,pi;
	int n;
	for(n=1; n<=2000000/2; n++)
	{
//		prod*=(2.0*n)/(2.0*n-1)*(2.0*n)/(2.0*n+1);
		prod*=(4.0*n*n)/(4.0*n*n-1);
	}
	pi=2*prod;
	printf("%f", pi);
	return 0;
}



(2)项数为奇数个,按奇数项和偶数项分别计算
π 2 = 2 1 ⋅ 2 3 ⋅ 4 3 ⋅ 4 5 ⋅ 6 5 ⋅ 6 7 ⋅ 8 7 ⋅ 8 9 ⋯ 奇数项 : a 1 = 2 1 a 3 = 4 3 a 5 = 6 5 a 7 = 8 7 ⋯   , 通项公式 a n = n + 1 n 偶数项 : a 2 = 2 3 a 4 = 4 5 a 6 = 6 7 a 8 = 8 9 ⋯   , 通项公式 a n = n n + 1 {\Large {\color{Black} \begin{align*} &\frac{\pi}{2}=\frac{2}{1} \cdot \frac{2}{3} \cdot \frac{4}{3} \cdot \frac{4}{5} \cdot \frac{6}{5} \cdot \frac{6}{7} \cdot \frac{8}{7} \cdot \frac{8}{9} \cdots \\ \\ &奇数项: a_{1}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{2}{1}\quad a_{3}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{4}{3}\quad a_{5}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{6}{5}\quad a_{7}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{8}{7}\cdots,通项公式a_{n}=\frac{n+1}{n}\\ \\ &偶数项: a_{2}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{2}{3}\quad a_{4}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{4}{5}\quad a_{6}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{6}{7}\quad a_{8}\hspace{-0.2cm}=\hspace{-0.1cm}\frac{8}{9}\cdots,通项公式a_{n}=\frac{n}{n+1} \end{align*} }} 2π=1232345456767898奇数项:a1=12a3=34a5=56a7=78,通项公式an=nn+1偶数项:a2=32a4=54a6=76a8=98,通项公式an=n+1n

#include<stdio.h>
#include<math.h>
int main()
{
	double prod=1,pi;
	int n;
	for(n=1; n<=1999999; n++)
	{
		if(n%2==1) //奇数
		{
			prod*=(n+1.0)/n;	
		}
		else //偶数
		{
			prod*=n/(n+1.0);
		}
	}
	pi=2*prod;
	printf("%f", pi);
	return 0;
}






3、巴塞尔问题

π 2 6 = 1 + 1 4 + 1 9 + 1 16 + ⋯ = 1 1 2 + 1 2 2 + 1 3 2 + 1 4 2 + ⋯ = ∑ n = 1 ∞ 1 n 2 {\Large {\color{Black} \begin{align*} \frac{\pi^{2}}{6} & = 1+\frac{1}{4}+\frac{1}{9}+\frac{1}{16}+\cdots \\[2ex] & = \frac{1}{1^{2}}+\frac{1}{2^{2}}+\frac{1}{3^{2}}+\frac{1}{4^{2}}+\cdots\\[2ex] & =\sum_{n=1}^{\infty} \frac{1}{n^{2}} \end{align*} }} 6π2=1+41+91+161+=121+221+321+421+=n=1n21

(1)输入阈值eps,直到最后一项的值小于给定阈值(包括最后一项)

do while循环:计算当前项,累加当前项,循环判断当前项

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=1,item,eps,pi;
	scanf("%lf", &eps);
	do
	{
		item=1.0/(n*n); //计算当前项
		sum+=item;      //累加当前项
		n++;
	}while(item>=eps);  //循环判断当前项
	pi=sqrt(6*sum);
	printf("%f", pi);
	return 0;
}



(2)输入阈值eps,直到最后一项的值小于给定阈值(不包括最后一项)

先提前计算第一项,然后再进入循环

while循环:循环判断当前项,累加当前项,计算下一项

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=1,item,eps,pi;
	scanf("%lf", &eps);
	item=1.0/(n*n);     //提前计算第一项
	while(item>=eps)    //循环判断当前项
	{
		sum+=item;      //累加当前项
		n++;
		item=1.0/(n*n); //计算下一项
	}
	pi=sqrt(6*sum);
	printf("%f", pi);
	return 0;
}

在这里插入图片描述





4、格雷戈里-莱布尼茨公式

π 4 = 1 − 1 3 + 1 5 − 1 7 + ⋯ = ( − 1 ) 0 1 + ( − 1 ) 1 3 + ( − 1 ) 2 5 + ( − 1 ) 3 7 + ⋯ = ∑ n = 0 ∞ ( − 1 ) n 2 n + 1 {\Large {\color{Black} \begin{align*} \frac{\pi}{4} & = 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\cdots \\[2ex] & = \frac{(-1)^{0}}{1}+\frac{(-1)^{1}}{3}+\frac{(-1)^{2}}{5}+\frac{(-1)^{3}}{7}+\cdots\\[2ex] & =\sum_{n=0}^{\infty} \frac{(-1)^{n}}{2n+1} \end{align*} }} 4π=131+5171+=1(1)0+3(1)1+5(1)2+7(1)3+=n=02n+1(1)n

(1)输入阈值eps,直到最后一项的值小于给定阈值(包括最后一项)

do while循环:计算当前项,累加当前项,循环判断当前项

因为当前项有正有负所以需要取绝对值,fabs()函数是<math.h>中的数学函数,它的功能就是取浮点数的绝对值。后面使用fabs()函数时不再重复说明

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=0,item,eps,pi;
	int sign=-1;
	scanf("%lf", &eps);
	do
	{
		sign=-sign;
		item=sign*1.0/(2*n+1); //计算当前项
		sum+=item;             //累加当前项
		n++;
	}while(fabs(item)>=eps);   //循环判断当前项
	pi=4*sum;
	printf("%f", pi);
	return 0;
}



(2)输入阈值eps,直到最后一项的值小于给定阈值(不包括最后一项)

先提前计算第一项,然后再进入循环

while循环:循环判断当前项,累加当前项,计算下一项

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=0,item,eps,pi;
	int sign=-1;
	scanf("%lf", &eps);
	sign=-sign;
	item=sign*1.0/(2*n+1);     //提前计算第一项
	while(fabs(item)>=eps)     //循环判断当前项
	{
		sum+=item;             //累加当前项
		n++;
		sign=-sign;
		item=sign*1.0/(2*n+1); //计算下一项
	};
	pi=4*sum;
	printf("%f", pi);
	return 0;
}





5、尼拉卡莎公式

π = 3 + 4 2 × 3 × 4 − 4 4 × 5 × 6 + 4 6 × 7 × 8 − 4 8 × 9 × 10 + ⋯ = 3 + ( − 1 ) 0 × 4 ( 2 × 1 ) × ( 2 × 1 + 1 ) × ( 2 × 1 + 2 ) + ( − 1 ) 1 × 4 ( 2 × 2 ) × ( 2 × 2 + 1 ) × ( 2 × 2 + 2 ) + ( − 1 ) 2 × 4 ( 2 × 3 ) × ( 2 × 3 + 1 ) × ( 2 × 3 + 2 ) + ( − 1 ) 3 × 4 ( 2 × 4 ) × ( 2 × 4 + 1 ) × ( 2 × 4 + 2 ) + ⋯ = 3 + ∑ n = 1 ∞ ( − 1 ) n − 1 × 4 2 n × ( 2 n + 1 ) × ( 2 n + 2 ) {\Large {\color{Black} \begin{align*} \pi&=3+\frac{4}{2 \times 3 \times 4}-\frac{4}{4 \times 5 \times 6}+\frac{4}{6 \times 7 \times 8}-\frac{4}{8 \times 9 \times 10}+\cdots\\[2ex] &=3+\frac{(-1)^{0} \times 4}{(2\times1) \times (2\times1+1) \times (2\times1+2)}+\frac{(-1)^{1} \times 4}{(2\times 2) \times (2\times2+1) \times (2\times2+2)}+\frac{(-1)^{2} \times 4}{(2\times3) \times (2\times3+1) \times (2\times3+2)}+\frac{(-1)^{3} \times 4}{(2\times4) \times (2\times4+1) \times (2\times4+2)}+\cdots\\[2ex] &=3+\sum_{n=1}^{\infty} \frac{(-1)^{n-1} \times 4}{2n \times (2n+1) \times (2n+2)} \end{align*} }} π=3+2×3×444×5×64+6×7×848×9×104+=3+(2×1)×(2×1+1)×(2×1+2)(1)0×4+(2×2)×(2×2+1)×(2×2+2)(1)1×4+(2×3)×(2×3+1)×(2×3+2)(1)2×4+(2×4)×(2×4+1)×(2×4+2)(1)3×4+=3+n=12n×(2n+1)×(2n+2)(1)n1×4
(1)输入阈值eps,直到最后一项的值小于给定阈值(包括最后一项)

do while循环:计算当前项,累加当前项,循环判断当前项(因为有正有负所以需要取绝对值)

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=1,item,eps,pi;
	int sign=-1;
	scanf("%lf", &eps);
	do
	{
		sign=-sign;
		item=sign*4.0/(2*n*(2*n+1)*(2*n+2)); //计算当前项
		sum+=item;                           //累加当前项
		n++;
	}while(fabs(item)>=eps);                 //循环判断当前项
	pi=3+sum;
	printf("%f", pi);
	return 0;
}



(2)输入阈值eps,直到最后一项的值小于给定阈值(不包括最后一项)

先提前计算第一项,然后再进入循环

while循环:循环判断当前项(因为有正有负所以需要取绝对值),累加当前项,计算下一项

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=1,item,eps,pi;
	int sign=-1;
	scanf("%lf", &eps);
	sign=-sign;
	item=sign*4.0/(2*n*(2*n+1)*(2*n+2));     //提前计算第一项
	while(fabs(item)>=eps)                   //循环判断当前项
	{
		sum+=item;                           //累加当前项
		n++;
		sign=-sign;
		item=sign*4.0/(2*n*(2*n+1)*(2*n+2)); //计算下一项
	}
	pi=3+sum;
	printf("%f", pi);
	return 0;
}





6、公式六(PTA 习题7-15)

π 2 = 1 + 1 3 + 2 ! 3 × 5 + 3 ! 3 × 5 × 7 + ⋯ + n ! 3 × 5 × 7 × ⋯ × ( 2 n + 1 ) + ⋯ = 0 ! 1 + 1 ! 1 × 3 + 2 ! 1 × 3 × 5 + 3 ! 1 × 3 × 5 × 7 + ⋯ + n ! 1 × 3 × 5 × 7 × ⋯ × ( 2 n + 1 ) + ⋯ {\Large {\color{Black} \begin{align*} \frac{\pi}{2} & = 1+\frac{1}{3}+\frac{2!}{3\times 5}+\frac{3!}{3\times 5\times 7}+\cdots+\frac{n!}{3\times 5\times 7\times \cdots \times (2n+1)}+\cdots \\[2ex] & = \frac{0!}{1}+\frac{1!}{1\times 3}+\frac{2!}{1\times 3\times 5}+\frac{3!}{1\times 3\times 5\times 7}+\cdots+\frac{n!}{1\times 3\times 5\times 7\times \cdots \times (2n+1)}+\cdots \end{align*} }} 2π=1+31+3×52!+3×5×73!++3×5×7××(2n+1)n!+=10!+1×31!+1×3×52!+1×3×5×73!++1×3×5×7××(2n+1)n!+

可以通过 an-1 和 an 得出递推公式

a n − 1 = ( n − 1 ) ! 1 × 3 × 5 × 7 × ⋯ × ( 2 n − 1 ) a n = n ! 1 × 3 × 5 × 7 × ⋯ × ( 2 n + 1 ) = ( n − 1 ) ! × n 1 × 3 × 5 × 7 × ⋯ × ( 2 n − 1 ) × ( 2 n + 1 ) 递推公式: a n = a n − 1 × n 2 n + 1 {\Large {\color{Black} \begin{align*} a_{n-1}&={\color{Blue} \frac{(n-1)!}{1\times 3\times 5\times 7\times \cdots \times (2n-1)}} \\ \\ a_{n}&=\frac{n!}{1\times 3\times 5\times 7\times \cdots \times (2n+1)}\\[2ex] &=\frac{{\color{Blue}(n-1)!}{\color{Red}\times n} }{{\color{Blue}1\times 3\times 5\times 7\times \cdots \times (2n-1)}{\color{Red} \times (2n+1)} }\\\\ &递推公式:a_{n}=a_{n-1}{\color{Red}\times \frac{n}{2n+1}} \end{align*} }} an1an=1×3×5×7××(2n1)(n1)!=1×3×5×7××(2n+1)n!=1×3×5×7××(2n1)×(2n+1)(n1)!×n递推公式:an=an1×2n+1n
(1)输入阈值eps,直到最后一项的值小于给定阈值(包括最后一项)

因为首项不适用递推公式,所以设首项的值为1,sum初值=首项,循环从第二项开始

do while循环:计算当前项,累加当前项,循环判断当前项

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=1,n=1,item=1,eps,pi; //sum初值=首项
	scanf("%lf", &eps);
	do
	{
		item*=n/(2*n+1); //计算当前项
		sum+=item;       //累加当前项
		n++;
	}while(item>=eps);   //循环判断当前项
	pi=2*sum;
	printf("%f", pi);
	return 0;
}



(2)输入阈值eps,直到最后一项的值小于给定阈值(不包括最后一项)

因为首项不适用递推公式,所以设首项的值为1,循环仍从首项开始

(此处不能从第二项开始,因为循环体第一步是累加当前项,如果从第二项开始,会先累加首项,让首项加两次,然后才计算第二项)

while循环:循环判断当前项,累加当前项,计算下一项

#include<stdio.h>
#include<math.h>
int main()
{
	double sum=0,n=0,item=1,eps,pi;
	scanf("%lf", &eps);
	while(item>=eps)     //循环判断当前项
	{
		sum+=item;       //累加当前项
		n++;
		item*=n/(2*n+1); //计算下一项
	}
	pi=2*sum;
	printf("%f", pi);
	return 0;
}

  • 8
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值