Pelles C forum

C language => Tips & tricks => Topic started by: JohnF on May 24, 2005, 11:57:56 AM

Title: Obfuscated PI
Post by: JohnF on May 24, 2005, 11:57:56 AM
You might sometimes come across obfuscated code - one area that can be badly obfuscated, IMO, is the 'for' loop. I found this routine that calculates PI to 800 decimal places.

Code: [Select]

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


Expanded a little

Code: [Select]

#include <stdio.h>
int main(void)
{
int a = 10000, b = 0, c = 2800, d = 0, e = 0, f[2801], g = 0;

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

printf("\n");
return 0;
}


It took me quite a while to figure out what actually is happening and write the routine in what I call normal code.

The reason for posting it? - do you really know the intricacies of the for loop? I didn't!

John
Title: Obfuscated PI
Post by: Pelle on May 24, 2005, 10:31:02 PM
I think C is about the only language with an "obfuscated code" contest. When looking at the first code example, I can understand why... ;-)

Pelle
Title: Obfuscated PI
Post by: JohnF on May 24, 2005, 10:47:31 PM
Quote from: "Pelle"
I think C is about the only language with an "obfuscated code" contest. When looking at the first code example, I can understand why... ;-)

Pelle


Yes, code should be simple to read IMO.

John