Mm. PI.

May 09, 2005 16:42

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);}
pi to 800 digits. In 160 characters of code. Woo! 0_o

Leave a comment

Well lambertbehnke May 9 2005, 16:18:01 UTC
Missing the decimal point of course,
and the last digit is the correct one but where you to finish your number there you'd round up.

Still fairly impressive,
beats the question though ... How did anyone find that out???

Oh well.

Lambert

(P.S.: And yes I did get the C compiler out and checked it ... sad I know)

Reply

Re: Well myrathi May 9 2005, 16:53:51 UTC
Yeah. To be specific, it should really insert the '.' and suffix '...' or something.

But still. Woo.

Reply

Re: Well lambertbehnke May 9 2005, 16:55:21 UTC
How did anyone work it out?

That's the question.

Reply

Re: Well myrathi May 9 2005, 16:57:10 UTC
It was an "obfuscated C" competition entry some time back. These people are just scary intelligent. :>

Reply

Re: Well doctorbutcher May 9 2005, 17:21:41 UTC
Either that or they waaay too much time on their hands...! :p

Reply

Re: Well myrathi May 9 2005, 18:08:19 UTC
That too. :)

I've been sitting here working out the maths behind what he did.
Makes sense, now. =)

Reply

Re: Well lambertbehnke May 10 2005, 08:12:48 UTC
Could you let the lesser mortals know here?

I have neither the time nor the will to work out how it works.

Reply

Re: Well myrathi May 10 2005, 09:20:40 UTC
Erm. Long story short? He fills an integer array up with pertinent values and then recomputes them at every pass, based on the "converging fractions" approach (since Pi is an irrational number, there's no 'final digit' or 'final answer' - everything's an approximation, at best).

It's long-winded and complex and made my head explode and I'm not doing it again. =P (I ditched the notepad file I was using to store values *twitch*)

I haven't actually tried it... but I'm fairly sure (from looking at his code) that you could probably change the 2800 and 2801 in the declarations at the start of the code to something larger (say, 9500 and 9501) and get a much closer approximation (with lots more digits).

You could probably get the loops that store and recompute f[] index values to output TDs in HTML so you could see the changes... but only if you wanted to fall asleep. ;)

Reply


Leave a comment

Up