當前位置:首頁 » 編程語言 » 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;

}

熱點內容
python做web開發 發布:2025-07-09 14:28:48 瀏覽:374
排序演算法代碼 發布:2025-07-09 14:27:59 瀏覽:501
存儲分類介紹 發布:2025-07-09 14:23:37 瀏覽:773
magento緩存 發布:2025-07-09 14:23:22 瀏覽:474
安卓機怎麼把時間弄在主頁面 發布:2025-07-09 14:17:28 瀏覽:1001
地產網站源碼 發布:2025-07-09 14:07:08 瀏覽:72
sdk3000編譯環境 發布:2025-07-09 14:06:09 瀏覽:979
煙灰奇跡腳本 發布:2025-07-09 14:02:27 瀏覽:76
游戲王伺服器地址 發布:2025-07-09 13:38:07 瀏覽:772
雙加密狗 發布:2025-07-09 13:26:51 瀏覽:472