现在的位置: 首页 > 综合 > 正文

四行代码计算圆周率800位的怪异程序(转)

2013年01月31日 ⁄ 综合 ⁄ 共 2474字 ⁄ 字号 评论关闭
求PI精确值的怪异程序
pi1.c(487bytes)/pi2.c(552bytes)

前者就是被称为「外星人程序」的求PI小程序..
四行求PI到小数点后八百位..真的不是人写得出来的.. :Q
后者写法也是相当奇怪..

---------------------------

/*某年Obfuscated C Contest佳作选录:*/

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);}

/*(原程序第一行是int a=10000,b,c....因为在IBM PC
上int只有2-byte,所以我改成long以便各platform都
能run)
能run)
(本程序连可算出pi值连 迭c前共800位)
(本程序节录自sci.math FAQ,原作者未详,我猜是外星人 :)*/

运行通过,答案正确

在 Unix 下:
//pi.c pi.c
#include
int 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);
}

Sun# gcc -o pi pi.c
Sun# ./pi
3141592653589793238462643383279502884197169399375105820974944592307816
4062862089
9862803482534211706798214808651328230664709384460955058223172535940812
8481117450
2841027019385211055596446229489549303819644288109756659334461284756482
3378678316
5271201909145648566923460348610454326648213393607260249141273724587006
6063155881
7488152092096282925409171536436789259036001133053054882046652138414695
1941511609
4330572703657595919530921861173819326117931051185480744623799627495673
5188575272
5188575272
4891227938183011949129833673362440656643086021394946395224737190702179
8609437027
7053921717629317675238467481846766940513200056812714526356082778577134
2757789609
1736371787214684409012249534301465495853710507922796892589235420199561
1212902196
0864034418159813629774771309960518707211349999998372978049951059731732
8160963185

vc下通过!
程序:
#include
#include
int 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);
return 0;
}
结果:
3141592653589793238462643383279502884197169399375105820974944592307816
4062862089
9862803482534211706798214808651328230664709384460955058223172535940812
8481117450
2841027019385211055596446229489549303819644288109756659334461284756482
3378678316
5271201909145648566923460348610454326648213393607260249141273724587006
6063155881
7488152092096282925409171536436789259036001133053054882046652138414695
1941511609
4330572703657595919530921861173819326117931051185480744623799627495673
5188575272
4891227938183011949129833673362440656643086021394946395224737190702179
8609437027
7053921717629317675238467481846766940513200056812714526356082778577134
2757789609
1736371787214684409012249534301465495853710507922796892589235420199561
1212902196
0864034418159813629774771309960518707211349999998372978049951059731732
8160963185

抱歉!评论已关闭.