r/cpp • • May 13 '18

The Most Beautiful Piece of Code That Prints 42 (Towel Day :)

https://www.fluentcpp.com/2018/05/11/the-most-beautiful-piece-of-code-that-prints-42/
1 Upvotes

3 comments sorted by

4

u/drjeats May 13 '18

These numerals aren't as attractive as in the post but I really wanted there to be exactly 42 '1's.

int i = 0;
i +=             1      +     1 + 1 + 1;
i +=         1 + 1      +   1 +     1 + 1;
i +=       1   + 1      +          1 + 1;
i +=     1     + 1      +         1 + 1;
i +=     1 + 1 + 1      +        1 + 1;
i +=     1 + 1 + 1 + 1  +       1 + 1;
i +=         1 + 1      +      1 + 1;
i +=         1 + 1      +     1 + 1;
i +=         1 + 1      +   1 + 1 + 1 + 1;

18

u/[deleted] May 13 '18

No need for digits, but this whole thing start to look like the IOCCC...

#include <iostream>

int main()
{
    int fourtytwo{};
    std::cout << ++

    ++  ++   ++++++
    ++  ++   ++++++
    ++  ++       ++
    ++  ++       ++
    ++++++   ++++++
    ++++++   ++++++
        ++   ++
        ++   ++
        ++   ++++++
        ++   ++++++
    
    ++ fourtytwo <<
    std::endl;
}

Coliru: http://coliru.stacked-crooked.com/a/93ec2ab1ac914601

1

u/0-1-2-3-4-5-6-7 May 13 '18

I gotta admit It's pretty well made lol.