69,373
社区成员
发帖
与我相关
我的任务
分享
n=6, pi=2.59807621
n=12, pi=3.00000000
n=24, pi=3.10582854
n=48, pi=3.13262861
n=96, pi=3.13935020
n=192, pi=3.14103195
n=384, pi=3.14145247
n=768, pi=3.14155761
n=1536, pi=3.14158389
n=3072, pi=3.14159046
n=6144, pi=3.14159211
n=12288,pi=3.14159252
n=24576,pi=3.14159262
#include <stdio.h>
#include <math.h>
int checkN(int n)
// if n is 6,12,24,48,96 return 1
// else return 0
{
int k=6;
while (k<n)
k*=2;
return (k==n);
}
void main()
{
double x0,h0,s0,x1,h1,s1;
int n,k;
printf("Please input n:");
scanf("%d",&n);
if (!checkN(n) )
{
printf("The n is wrong!/n");
return ;
}
k=6;
x0=1; //6边形的边长
h0=sqrt(3)/2.0; //6边形圆心到边的高
s0= k * x0 * h0 /2; //6边形的面积
while (k<n)
{
x1= sqrt( (x0/2)*(x0/2) + (1-h0) * (1-h0) ); // 递推计算S(2k), H(2k), x(2k)
h1= sqrt(1- (x1/2.0) * (x1/2.0));
s1=s0 + k *0.5*x0* (1.0-h0);
k*=2;
x0=x1;
h0=h1;
s0=s1;
}
//因为 s = pi * r ^ 2,
//故pi= s/(r^2),这里r=1; 故 pi=s;
printf("pi=%.8lf\n",s0); //祖冲之计算到24576
}
#include <stdio.h>
#include <math.h>
int checkN(int n)
// if n is 6,12,24,48,96 return 1
// else return 0
{
int k=6;
while (k<n)
k*=2;
return (k==n);
}
void main()
{
double sinx,cosx,pi,t;
int n,k;
printf("Please input n:");
scanf("%d",&n);
if (!checkN(n) )
{
printf("The n is wrong!/n");
return ;
}
sinx=0.5; //sin 30度
cosx=sqrt(1.0-sinx*sinx); //cos 30度
for(k=6;k<n;k*=2)
{
t=cosx;
cosx= sqrt((1+ t)/2); //余弦半角公式
sinx= sqrt((1 -t)/2); //正弦半角公式
}
pi= k * sinx;
printf("pi=%.6lf\n",pi);
}
n=6, pi=3.000000
n=12, pi=3.105829
n=24, pi=3.132629
n=48, pi=3.139350
n=96, pi=3.141032
n=192, pi=3.141452
n=384, pi=3.141558
n=768, pi=3.141584
n=1536,pi=3.141590