一个让人发狂的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

这个程序更有意思了,可惜我还不明白其中的原理。希望大家讨论一下,谢谢 !

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