what this program do

  • Thread starter Three Headed Monkey
  • Start date
T

Three Headed Monkey

What does bellow C program calculate?
I tried my best but couldn't figured it out.

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4ld",e+d/a),e=d%a)
for(b=c;d+=f*a,f=d%--g,d/=g--,--b;d*=b);
}
 
M

Mark McIntyre

Three said:
What does bellow C program calculate?

Nothing useful. Looks like either an entry in the obfuscated C contest,
or a good example of an overly-smart programmer abusing the language.
I tried my best but couldn't figured it out.

Why not run it and see what it produces?

That said, there's no point - you will never willingly write code like
this, and if you find it in a programme you're maintaining you should
rip it out asap.
 
J

jacob navia

Three said:
What does bellow C program calculate?
I tried my best but couldn't figured it out.

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4ld",e+d/a),e=d%a)
for(b=c;d+=f*a,f=d%--g,d/=g--,--b;d*=b);
}


The output of that is
0314015902650358097903230846026403380327095002880419071609390937051005820097049404590230078106400628062008990862080304820534021107060798021408080651032802300664
0709038404600955005802230172053509400812084801110745002804100270019308520110055509640462029408950493003801960442088100970566059303440612084705640823037806780316
0527012001900914056408560692034600340861004504320664082103390360072600240914012703720458070006600631055808170488015200920096028209250409017105360436078902590036
0001013300530054088200460652013804140695019401510160094303050727003605750959019503090218061107380193026101790310051108540807044602370996027409560735018805750272
0489012207930818030101940912098303670336024400650664030806020139049406390522047307190070021709860094037002770053092107170629031706750238046704810846076609400513

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...

The number PI is:

3.141592653589793238462643383279502884197169399375...
 
M

Morris Dovey

jacob said:
The output of that is
0314015902650358097903230846026403380327095002880419071609390937051005820097049404590230078106400628062008990862080304820534021107060798021408080651032802300664
0709038404600955005802230172053509400812084801110745002804100270019308520110055509640462029408950493003801960442088100970566059303440612084705640823037806780316
0527012001900914056408560692034600340861004504320664082103390360072600240914012703720458070006600631055808170488015200920096028209250409017105360436078902590036
0001013300530054088200460652013804140695019401510160094303050727003605750959019503090218061107380193026101790310051108540807044602370996027409560735018805750272
0489012207930818030101940912098303670336024400650664030806020139049406390522047307190070021709860094037002770053092107170629031706750238046704810846076609400513

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...

The number PI is:

3.141592653589793238462643383279502884197169399375...

Aha! Good catch!

I changed the format specifier and added a final newline:

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%ld",e+d/a),e=d%a)
for(b=c;d+=f*a,f=d%--g,d/=g--,--b;d*=b);
printf("\n");
}

Which prints:

314159265358979323846264338327950288419716939937510582974944592307816406286208998628034825342117067982148086513282306647093844609555822317253594081284811174528410270193852110555964462294895493381964428819756659334461284756482337867831652712019091456485669234634861454326648213393607262491412737245870066063155881748815292962829254091715364367892593611335354882466521384146951941511609433057273657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624465664308602139494639522473719702179869437027753921717629317675238467481846766940513

:)
 
P

Peter Nilsson

Morris Dovey said:
jacob said:
The output of that is
031401590265035...

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...
...
I changed the format specifier and added a final newline:

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%ld",e+d/a),e=d%a)

It needs to be "%.3ld".
for(b=c;d+=f*a,f=d%--g,d/=g--,--b;d*=b);
printf("\n");

}

Which prints:

31415926...58297...


Which is wrong because it should be ...582097...
 
T

Three Headed Monkey

Three said:
What does bellow C program calculate?
I tried my best but couldn't figured it out.

#include <stdio.h>
main()
{
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4ld",e+d/a),e=d%a)
for(b=c;d+=f*a,f=d%--g,d/=g--,--b;d*=b);
}


The output of that is
03140159026503580979032308460264033803270950028804190716093909370510058200970494
04590230078106400628062008990862080304820534021107060798021408080651032802300664
07090384046009550058022301720535094008120848011107450028041002700193085201100555
09640462029408950493003801960442088100970566059303440612084705640823037806780316
05270120019009140564085606920346003408610045043206640821033903600726002409140127
03720458070006600631055808170488015200920096028209250409017105360436078902590036
00010133005300540882004606520138041406950194015101600943030507270036057509590195
03090218061107380193026101790310051108540807044602370996027409560735018805750272
04890122079308180301019409120983036703360244006506640308060201390494063905220473
07190070021709860094037002770053092107170629031706750238046704810846076609400513

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...

The number PI is:

3.141592653589793238462643383279502884197169399375...


Thank you Jakob, that is very useful information.

I think error may be author wanted to print space instead of zero in
printf format. Or a should be 10000 and compute 4 digits every loop.

Is this known algorithm? What is name of algorithm? All algorithms in
WWW are very complicated and long, but this algorithm is complicated and
short and I like it. How does it work? Code seems not very clear. If I
use long long, how many digits can it print?

What is limit that can be done in C language?
 
C

CBFalconer

Peter said:
Morris Dovey said:
jacob said:
The output of that is 031401590265035...

If we take some zeroes out (it has a bug maybe) we get

31415926535897932384626433832795028841971 ...
...
I changed the format specifier and added a final newline:

#include <stdio.h>
main() {
long a=1000,b=0,c=2800,d,e=0,f[2801],g;
for(;b-c;) f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%ld",e+d/a),e=d%a)

It needs to be "%.3ld".
for(b=c;d+=f*a,f=d%--g,d/=g--,--b;d*=b);
printf("\n");
}

Which prints:

31415926...58297...


Which is wrong because it should be ...582097...


Which the corrected version (with a = 10000) does generate. I
haven't the vagues idea of the algorithm.
 
R

Richard Tobin

I haven't the vaguest idea of the algorithm.

See http://www.mathpropress.com/stan/bibliography/spigot.pdf

In short: if you represent pi in the "mixed-radix" base where the
values for each digit overflow at

. 1/3 2/5 3/7 4/9 ...

(that is, the first digit after the point represents thirds, the
second multiples of 1/3 * 2/5, and so on),

then the representation of pi is

2 . 2 2 2 2 ...

and the program works by converting that to base 10 (actually, 10000).

A similar algorithm for calculating the digits of e - also given in
the same paper - is much easier to understand.

-- Richard
 
D

Dik T. Winter

> I think error may be author wanted to print space instead of zero in
> printf format. Or a should be 10000 and compute 4 digits every loop.

Right. 10000 and %.4d. At least that is what my .plan says.
> Is this known algorithm?

Eh, yes. It uses a mixed base representation of pi.
 
M

Morris Dovey

Richard said:

Thanks for the link!

I got to thinking about how one might go about using the pi and e
digit strings to build a random number generator. Seemed like a
good idea at the time. :)

Then I wrote a tiny program to give me the digit frequencies and
decided that the idea might not be as good as I'd thought it
might be. With a initialized to 10000 and the printf() format
changed to "%.3ld" to produce the digits, I ran my little counter
program to get:

0 56
1 92
2 83
3 80
4 80
5 73
6 77
7 75
8 76
9 90

Which surprised me because I'd expected a much more even
distribution.
 
D

Dik T. Winter

>
> See http://www.mathpropress.com/stan/bibliography/spigot.pdf ....
> A similar algorithm for calculating the digits of e - also given in
> the same paper - is much easier to understand.

Right. When I wrote the program originally with a cow-orker the idea
was to see how the algorithm we knew for a long time for 'e' work out
for 'pi'. However, the algorithm for 'pi' is much shakier than that
for 'e'. In order to let it work correctly you should ensure that the
"digits" are always within certain bounds. For 'e' that is simple, it
is assured by the series development for 'e'. For 'pi' that is not the
case. And indeed, if you increase the arrays and the loop length etc.
you will find that at a certain point the program will start to give
wrong results. Once we had the program working I thought to decrease
its size and obfuscate the program as much as possible (although the
decrease in size was the most important for me). It had gone to a point
where even the C-beautifier of that time did not know what to do with it.
 
D

Dik T. Winter

> I got to thinking about how one might go about using the pi and e
> digit strings to build a random number generator. Seemed like a
> good idea at the time. :)
>
> Then I wrote a tiny program to give me the digit frequencies and
> decided that the idea might not be as good as I'd thought it
> might be. With a initialized to 10000 and the printf() format
> changed to "%.3ld" to produce the digits, I ran my little counter
> program to get: ....
> Which surprised me because I'd expected a much more even
> distribution.

You would have gotten that with the format string "%.4d". On the other
hand, using pi and e for random digits only works when they are normal
to base 10 (and there must be some additional randomness in the digits).
It is unknown whether they are.
 
M

Morris Dovey

Dik T. Winter said:
You would have gotten that with the format string "%.4d". On the other
hand, using pi and e for random digits only works when they are normal
to base 10 (and there must be some additional randomness in the digits).
It is unknown whether they are.

I tried that, with this result:

0 74
1 92
2 83
3 80
4 80
5 73
6 77
7 75
8 76
9 90

which is more uniform, but less so than I'd expected.

The "digit spigot" is very interesting!

Thanks
 
R

Richard

Morris Dovey said:
Thanks for the link!

I got to thinking about how one might go about using the pi and e
digit strings to build a random number generator. Seemed like a
good idea at the time. :)

Then I wrote a tiny program to give me the digit frequencies and
decided that the idea might not be as good as I'd thought it
might be. With a initialized to 10000 and the printf() format
changed to "%.3ld" to produce the digits, I ran my little counter
program to get:

0 56
1 92
2 83
3 80
4 80
5 73
6 77
7 75
8 76
9 90

Which surprised me because I'd expected a much more even
distribution.

Looks even enough to me considering the numbers involved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top