The unity of form and content

Here one programmer sent me a funny round program in C, there was #define defining an underline, and then a circle was drawn from underscores and minuses:

            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
        _-_-_-_-_-_-_-_
            _-_-_-_


When calculating this expression, the number π was obtained. Well, in fact, something moved out there and instead of π it turned out 0.250, but that's not the point.



At first, I wanted to “debug” this program, but I thought that there are actually many programs with a picture drawn in them. But you can give it a geometric meaning!

It is said - done, here is a program for calculating the number π:
#define O o ++;
#define K k ++;
double o = 0;
double k = 0;
main ()
{
          OOOOOOKKKKKKKKK
      OOOOOOOOOOKKKKKKKK
    OOOOOOOOOOOOKKKKKKKK
    OOOOOOOOOOOOKKKKKKKK
  OOOOOOOOOOOOOOKKKKKKK K
  OOOOOOOOOOOOOOKKKKKKK K
  OOOOOOOOOOOOOOKKKKKKK K
OOOOOOOOOOOOOOOOKKKKK KKK
OOOOOOOOOOOOOOOOKKKKK KKK
OOOOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOOO
  OOOOOOOOOOOOOOO
  OOOOOOOOOOOOOOO
  OOOOOOOOOOOOOOO
    OOOOOOOOOOOO
    OOOOOOOOOOOO
      OOOOOOOOOO
          OOOOOO
printf ("% f \ n", o / k);
}


It turns out 3.1666 ... but if you draw a bigger picture, then the accuracy will increase.

The area of ​​the circle is πR², that is, if we divide the area of ​​the circle by the area of ​​the square, the side of which is equal to the radius of this circle, we get π. We take two variables and draw a circle with increments of one variable, and a square with increments of another. As a result, the ratio of these variables is equal to the ratio of the area of ​​the figures.

UPDATE : Actually, the program that inspired me also ended up with a geometric meaning.
Her story and full text can be found here: International Obfuscated C Code Contest ,
and the comments are in the comments on this post.

However, he slipped away from me. By the way, in the comments there is an option corrected for modern preprocessors, but in it the geometric meaning is already partially lost, since the diameter of the circle is already sewn into a constant (0xF).

It remains to sprinkle ashes on my head and be glad that my version is more transparent, and that I, although not directly (and not even realizing it), have alluded to the original source of the idea.

Also popular now: