C language > Tips & tricks
Obfuscated PI
(1/1)
JohnF:
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: ---
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);}
--- End code ---
Expanded a little
--- Code: ---
#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;
}
--- End code ---
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
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
JohnF:
--- 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
--- End quote ---
Yes, code should be simple to read IMO.
John
Navigation
[0] Message Index
Go to full version