求教一个计算圆周率的最优算法

dengcainiao 2002-05-10 07:05:28
写谢谢
...全文
10 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Tice 2002-05-20
那要算多久?
回复
qtf 2002-05-12
这么厉害!!!
10亿?
回复
xuying 2002-05-11
上面的程序可以算任意位。
回复
xuying 2002-05-11
#include<stdio.h>
#include<stdlib.h>

#define Fcalloc calloc
#define Ffree free
#define Size_T unsigned long

long kf, ks;
long *mf, *ms;
long cnt, n, temp, nd;
long i;
long col, col1;
long loc, stor[21];

void shift(long *l1, long *l2, long lp, long lmod) {
long k;

k = ((*l2) > 0 ? (*l2) / lmod: -(-(*l2) / lmod) - 1);
*l2 -= k * lmod;
*l1 += k * lp;
}

void yprint(long m) {
if (cnt<n) {
if (++col == 11) {
col = 1;
if (++col1 == 6) {
col1 = 0;
printf("\n");
printf("%4ld",m%10);
}
else printf("%3ld",m%10);
}
else printf("%ld",m);
cnt++;
}
}

void xprint(long m) {
long ii, wk, wk1;

if (m < 8) {
for (ii = 1; ii <= loc; )
yprint(stor[(int)(ii++)]);
loc = 0;
}
else {
if (m > 9) {
wk = m / 10;
m %= 10;
for (wk1 = loc; wk1 >= 1; wk1--) {
wk += stor[(int)wk1];
stor[(int)wk1] = wk % 10;
wk /= 10;
}
}
}
stor[(int)(++loc)] = m;
}


void memerr(int errno) {
printf("\a\nOut of memory error #%d\n", errno);
if (2 == errno)
Ffree(mf);
_exit(2);
}

int main(int argc, char *argv[]) {
int i=0;
char *endp;

printf("\n");
printf("PI Caculater made by Godman\n");
printf("$Id: pi.c 1.14 1998/12/03 15:04:23 GUOXM Exp $\n");
printf("==============================================\n");

stor[i++] = 0;
if (argc < 2) {
puts("\aUsage: PI <number_of_digits>");
return(1);
}
n = strtol(argv[1], &endp, 10);
if (NULL == (mf = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
memerr(1);
if (NULL == (ms = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
memerr(2);
printf("\nApproximation of PI to %ld digits\n", (long)n);

cnt = 0;
kf = 25;
ks = 57121L;
mf[1] = 1L;

for (i = 2; i <= (int)n; i += 2) {
mf[i] = -16L;
mf[i+1] = 16L;
}

for (i = 1; i <= (int)n; i += 2) {
ms[i] = -4L;
ms[i+1] = 4L;
}

printf("\n 3.");
while (cnt < n) {
for (i = 0; ++i <= (int)n - (int)cnt; ) {
mf[i] *= 10L;
ms[i] *= 10L;
}
for (i =(int)(n - cnt + 1); --i >= 2; ) {
temp = 2 * i - 1;
shift(&mf[i - 1], &mf[i], temp - 2, temp * kf);
shift(&ms[i - 1], &ms[i], temp - 2, temp * ks);
}
nd = 0;
shift((long *)&nd, &mf[1], 1L, 5L);
shift((long *)&nd, &ms[1], 1L, 239L);
xprint(nd);
}
printf("\n\n======================\n");
printf("Calculations Completed!\n");
printf("You can fine this program and source code in ftp://166.111.18.56\n");
Ffree(ms);
Ffree(mf);
return(0);
}
回复
kingfighter_han 2002-05-11
o
回复
NowCan 2002-05-11
10亿位的都见过。
回复
Tice 2002-05-10
我又看过了,最多可以算到3335万位
回复
Tice 2002-05-10
我知道有个叫super pi的程序,可以算到100多万位
回复
one_add_one 2002-05-10
最优的我不知道,比较好的,我到时知道几个。
回复
atlantis13579 2002-05-10
肯定不是最优算法,但可以算.

long a=10000,b,c=2800,d,e,f[2801],g;
main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}
回复
相关推荐
发帖
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2002-05-10 07:05
社区公告
暂无公告