四行代碼計算圓周率800位的怪異程式)

來源:互聯網
上載者:User
求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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.