📜 ⬆️ ⬇️

Unity of form and content

A programmer sent me a funny round C program here, there was a #define defining underline, and then a circle was drawn from underscores and minuses:

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


When calculating this expression, we get the number π. 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 were a lot of such programs, where some kind of picture was drawn with code. But you can give this geometric meaning!

No sooner said than done, here is a program to calculate the number π:
 #define O o ++;
 #define K k ++;
 double o = 0;
 double k = 0;

 main ()
 {

           OOOOOOKKKKKKKK
       OOOOOOOOOOKKKKKKKK
     OOOOOOOOOOOOKKKKKKKK
     OOOOOOOOOOOOKKKKKKKK
   OOOOOOOOOOOOOOKKKKKKK K
   OOOOOOOOOOOOOOKKKKKKK K
   OOOOOOOOOOOOOOKKKKKKK K
 OOOOOOOOOOOOOOOOKKKKK KKK
 OOOOOOOOOOOOOOOOKKKKK KKK
 OOOOOOOOOOOOOOOO
 OOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOO
   OOOOOOOOOOOOOO
   OOOOOOOOOOOOOO
     OOOOOOOOOOOO
     OOOOOOOOOOOO
       OOOOOOOOOO
           OOOOOO

 printf ("% f \ n", o / k);
 } 


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

The area of ​​a circle is πR², that is, if you divide the area of ​​a circle by the area of ​​a square whose side is equal to the radius of this circle, you get π. 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 areas of the figures.

UPDATE : Actually, the program that inspired me also turned out to be with a geometric meaning.
Her story and full text can be found here: International Obfuscated C Code Contest ,
And explanations are in comments to 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, though not directly (and even unaware of it), referred to the original source of the idea.

Source: https://habr.com/ru/post/209244/


All Articles