一個讓人發狂的PI求解C程序

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); scanf("%s");}

簡短的4行代碼,就可以精確計算機出800位的PI(圓周率)值。
實在太震撼人心了。這樣的程序也能運行,竟然還能能完成這樣讓人難以置信的任務,真是太神了。
真讓人百思不知其解了。不知道它是採用了哪一個級數公式,收斂速度如此之快?在《程序員》第十一期中還專門提供了這個程序源碼。喜歡挑戰的朋友請來試一試哦?

這是某一年The International Obfuscated C Code Contest(國際模糊C代碼大賽)上的獲獎作品(努力了,但是沒有找到一個確切的時間)。這是屬於C大師的盛會,因爲這是一件極具挑戰的活兒。

這是Linux/Unix/... 下的版本,需要更改才能在Windows平臺上運行。


The International Obfuscated C Code Contest 的目標是:

在以下幾條規則的前提下,編寫晦澀難懂的(模糊的、混亂的)C程序。
1、通過一種諷刺的手段,來說明編程風格的重要性;
2、用怪異的代碼來調試C編譯器;
3、舉例說明一些C語言的薄弱或精明之處;
4、爲脆弱的C代碼提供一個安全的討論會。

Goals of the Contest

Obfuscate: tr.v. -cated, -cating, -cates. 1. a. To render obscure.
b. To darken. 2. To confuse: his emotions obfuscated his judgment.
[LLat. obfuscare, to darken : ob(intensive) + Lat. fuscare,
to darken < fuscus, dark.] -obfuscation n. obfuscatory adj


To write the most Obscure/Obfuscated C program under the rules below.
To show the importance of programming style, in an ironic way.
To stress C compilers with unusual code.
To illustrate some of the subtleties of the C language.
To provide a safe forum for poor C code. :-)

請訪問下面的官方網站,如果希望瞭解更多的相關信息。

http://www.ioccc.org/
http://www.au.ioccc.org/

Pi to one MILLION decimal places

http://3.141592653589793238462643383279502884197169399375105820974944592.com/

二、 以下是另一個版本的程序。一樣晦澀難懂。(1988年)

Here''s another good one:

/*
 * Program to compute an approximation of pi
 * by Brian Westley, 1988
 * (requires pcc macro concatenation; try gcc -traditional-cpp)
 */

#define _ -F<00||--F-OO--;
int F=00,OO=00;
main(){F_OO();printf("%1.3f/n",4.*-F/OO/OO);}F_OO()
{
            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
        _-_-_-_-_-_-_-_
            _-_-_-_
}

http://www.comedia.com/hot/jargon-4.2.3/html/entry/Obfuscated-C-Contest.html

這個程序更有意思了,可惜我還不明白其中的原理。希望大家討論一下,謝謝 !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章