當前位置:首頁 » 編程語言 » c語言pi計算

c語言pi計算

發布時間: 2024-05-31 20:32:42

① 用c語言求出圓周率,這是一個神奇的程序。求解釋

這個比較出名的程序,三行語句求PI

按公式PI/2=∑(n!/(2n+1)!!) 計算Pi

計算2800項就可以精確到小數點後800位

正常的寫法

#include"stdio.h"
longb,c,d,e,f[2801];
voidmain()
{
for(inti=0;i<2800;i++)f[i]=2000;
f[2800]=0;
for(c=2800;c>0;c-=14)
{
d=0;
for(b=c;b>0;b--)
{
d+=f[b]*10000;
f[b]=d%(2*b-1);
d/=(2*b-1);
if(b>1)
d*=(b-1);
}
printf("%.4d",e+d/10000);
e=d%10000;
}
}

② C語言計算pi

在0到1之間取兩個隨機數,如果這兩個隨機數(x,y)在四分之一圓內,就加一。
最後用落在圓內的點數,除以總點數,就是PI了。
#include
<stdio.h>
#include
<conio.h>
#include
<stdlib.h>
#include
<time.h>
#define
N
300000
main()
{
long
i,n=0;
float
x,y,pi;
srand(time(NULL));
for
(i=0;i<N;i++)
{
x=1.0*rand()/RAND_MAX;
y=1.0*rand()/RAND_MAX;
if
(x*x+y*y<1)
n++;
}
pi=4.0*n/N;
printf("pi=%f
",pi);
}

③ C語言:計算圓周率(精度保留到小數點後6位),用程序實現,公式如下:

#include<stdio.h>

#include<math.h>

int main()

{double pi=1,t=0;

for(;t<1.9999999;)

{t=sqrt(2+t);

pi*=2/t;

}

printf("%lf ",pi*2);

return 0;

}

熱點內容
紅米手機存儲問題 發布:2025-07-04 18:50:43 瀏覽:840
水電煤演算法 發布:2025-07-04 18:36:44 瀏覽:329
天翼視訊緩存文件夾 發布:2025-07-04 18:36:43 瀏覽:96
unix網路編程第2卷 發布:2025-07-04 18:33:41 瀏覽:781
編譯歷史 發布:2025-07-04 18:23:01 瀏覽:851
空調壓縮機種類 發布:2025-07-04 18:13:58 瀏覽:242
中國有ip6伺服器嗎 發布:2025-07-04 17:58:56 瀏覽:726
第六章編譯原理答案 發布:2025-07-04 17:37:55 瀏覽:40
php內存優化 發布:2025-07-04 17:25:54 瀏覽:664
威綸觸摸屏如何設置時間限制密碼 發布:2025-07-04 17:25:50 瀏覽:418