因?yàn)閳A面積s=pi*r^2,
創(chuàng)新互聯(lián)長(zhǎng)期為近千家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為通江企業(yè)提供專(zhuān)業(yè)的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè),通江網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
所以pi=s/r^2.
點(diǎn)(x,y)是0~1正方形范圍內(nèi)的隨機(jī)點(diǎn),如果x^2+y^2=1,則該點(diǎn)在單位圓內(nèi).
x^2+y^2就是r^2,即圓半徑的平方.d/N為點(diǎn)x,y落入單位圓內(nèi)的概率,即約為圓面積.
圓的半徑為1/2,而不是1,所以pi=4*d/N.
#include?stdio.h
double?fact(int?n)???
{?
double?res?=?1.0;
for(int?i?=?1;?i?=?n;?i++)??
res?*=?i;
return?res;?
}
double?multi(int?n)??
{?
double?res?=?1.0;
for(int?i?=?1;?i?=?n;?i?+=?2)
res?*=?i;
return?res;??
}
int?main()
{
double?sum=1.0,?item=1.0,?eps?=?1e-6;
for(int?i?=?1;?item?=?eps;?i++)
{
item?=?fact(i)?/?multi(2*i?+?1);
sum?=?sum?+?item;
}?????
printf("PI=%0.5lf\n",?sum?*?2);
return?0;
}
//這樣可以得到3.14159
在for循環(huán)后加一句: sum -= f;
你多加了最后那個(gè)小于epsioon的值。
#include?stdio.h
int?main()
{
float?f;
double?pi,i,sign;
while(scanf("%f",f)==1)
{
pi=0;
i=1;
sign=1;
do
{
pi+=sign*1.0/i;
}while(1.0/i=f(sign=-sign)(i+=2));
printf("%lf\n",pi*4.0);
}
return?0;
}
#include stdio.h
#define L 10000 //求10000位PI值
#define N L/4+1
// L 為位數(shù),N是array長(zhǎng)度
/*圓周率后的小數(shù)位數(shù)是無(wú)止境的,如何使用電腦來(lái)計(jì)算這無(wú)止境的小數(shù)是一些數(shù)學(xué)家與程式設(shè)計(jì)師所感興趣的,在這邊介紹一個(gè)公式配合 大數(shù)運(yùn)算,可以計(jì)算指定位數(shù)的圓周率。
John Wallis的圓周率公式:
//詳細(xì)看網(wǎng)站介紹:
PI = [16/5 - 16 / (3*53) + 16 / (5*55) - 16 / (7*57) + ......] - [4/239 - 4/(3*2393) + 4/(5*2395) - 4/(7*2397) + ......]
*/
void add ( int*, int*, int* );
void sub ( int*, int*, int* );
void div ( int*, int, int* );
int main ( void )
{
int s[N+3] = {0};
int w[N+3] = {0};
int v[N+3] = {0};
int q[N+3] = {0};
int n = ( int ) ( L/1.39793 + 1 );
int k;
w[0] = 16*5;
v[0] = 4*239;
for ( k = 1; k = n; k++ )
{
// 套用公式
div ( w, 25, w );
div ( v, 239, v );
div ( v, 239, v );
sub ( w, v, q );
div ( q, 2*k-1, q );
if ( k%2 ) // 奇數(shù)項(xiàng)
add ( s, q, s );
else // 偶數(shù)項(xiàng)
sub ( s, q, s );
}
printf ( "%d.", s[0] );
for ( k = 1; k N; k++ )
printf ( "%04d", s[k] );
printf ( "\n" );
return 0;
}
void add ( int *a, int *b, int *c )
{
int i, carry = 0;
for ( i = N+1; i = 0; i-- )
{
c[i] = a[i] + b[i] + carry;
if ( c[i] 10000 )
carry = 0;
else // 進(jìn)位
{
c[i] = c[i] - 10000;
carry = 1;
}
}
}
void sub ( int *a, int *b, int *c )
{
int i, borrow = 0;
for ( i = N+1; i = 0; i-- )
{
c[i] = a[i] - b[i] - borrow;
if ( c[i] = 0 )
borrow = 0;
else // 借位
{
c[i] = c[i] + 10000;
borrow = 1;
}
}
}
void div ( int *a, int b, int *c ) // b 為除數(shù)
{
int i, tmp, remain = 0;
for ( i = 0; i = N+1; i++ )
{
tmp = a[i] + remain;
c[i] = tmp / b;
remain = ( tmp % b ) * 10000;
}
}