what this program do

Discussion in 'C Programming' started by Three Headed Monkey, Mar 26, 2008.

  1. 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);
    }
     
    Three Headed Monkey, Mar 26, 2008
    #1
    1. Advertising

  2. Three Headed Monkey wrote:
    > 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.
     
    Mark McIntyre, Mar 26, 2008
    #2
    1. Advertising

  3. Three Headed Monkey

    jacob navia Guest

    Three Headed Monkey wrote:
    > 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...
    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Mar 27, 2008
    #3
  4. In article <fseoja$97a$>, jacob navia <> wrote:

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


    It does have a bug; a should be initialised to 10000 not 1000. It's
    a faulty transcription of Dik Winter's program here:

    http://groups.google.co.uk/group/alt.lang.intercal/msg/82b16b9bd6e03e51

    -- Richard
    --
    :wq
     
    Richard Tobin, Mar 27, 2008
    #4
  5. Three Headed Monkey

    Morris Dovey Guest

    jacob navia wrote:

    > 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

    :)

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Mar 27, 2008
    #5
  6. Morris Dovey <> wrote:
    > jacob navia wrote:
    > > 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...

    --
    Peter
     
    Peter Nilsson, Mar 27, 2008
    #6
  7. On Thu, 27 Mar 2008 01:09:11 +0100, jacob navia <> wrote:

    > Three Headed Monkey wrote:
    > > 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?
     
    Three Headed Monkey, Mar 27, 2008
    #7
  8. Three Headed Monkey

    CBFalconer Guest

    Peter Nilsson wrote:
    > Morris Dovey <> wrote:
    >> jacob navia wrote:
    >>
    >>> 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Mar 27, 2008
    #8
  9. In article <>,
    CBFalconer <> wrote:
    >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
    --
    :wq
     
    Richard Tobin, Mar 27, 2008
    #9
  10. In article <ab12159$RnVYyIU6$> Three Headed Monkey <> writes:
    > 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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Mar 27, 2008
    #10
  11. Three Headed Monkey

    Morris Dovey Guest

    Richard Tobin wrote:

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


    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.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Mar 27, 2008
    #11
  12. In article <fsg4bp$24pj$> (Richard Tobin) writes:
    > In article <>,
    > CBFalconer <> wrote:
    > >I haven't the vaguest idea of the algorithm.

    >
    > 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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Mar 27, 2008
    #12
  13. In article <> Morris Dovey <> writes:
    ....
    > 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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Mar 27, 2008
    #13
  14. Three Headed Monkey

    Morris Dovey Guest

    "Dik T. Winter" wrote:
    >
    > In article <> Morris Dovey <> writes:


    > > 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.


    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

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Mar 27, 2008
    #14
  15. In article <>,
    Morris Dovey <> wrote:
    >which is more uniform, but less so than I'd expected.


    See http://mathworld.wolfram.com/PiDigits.html, which includes a table
    of the distribution of digits in the first 10^n places for n up to 12,
    and says it "shows no statistically significant departure from a
    uniform distribution".

    -- Richard
    --
    :wq
     
    Richard Tobin, Mar 27, 2008
    #15
  16. Three Headed Monkey

    Richard Bos Guest

    (Richard Tobin) wrote:

    > In article <>,
    > CBFalconer <> wrote:
    > >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).


    Very clever.

    Richard
     
    Richard Bos, Mar 27, 2008
    #16
  17. Three Headed Monkey

    Richard Guest

    Morris Dovey <> writes:

    > Richard Tobin wrote:
    >
    >> See http://www.mathpropress.com/stan/bibliography/spigot.pdf

    >
    > 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.
     
    Richard, Mar 27, 2008
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pipi
    Replies:
    1
    Views:
    640
    Joseph Millar
    Jul 21, 2003
  2. Rey
    Replies:
    4
    Views:
    11,073
    Roedy Green
    Dec 12, 2003
  3. ola
    Replies:
    3
    Views:
    729
    Marco Schmidt
    Feb 16, 2004
  4. Parvsandhu
    Replies:
    2
    Views:
    430
    Thomas Weidenfeller
    Jul 11, 2005
  5. Replies:
    1
    Views:
    545
    Andrew Thompson
    Oct 31, 2005
Loading...

Share This Page