Well, you are reading this so you must not have figured out a cool short C program on your own, or taken the trouble to compile it. No worries, I'm lazy too. The output is the Mandelbrot set:

1111111111111111222222222222222222222222222222222222222222222222222222222222222
1111111111111122222222222222222222222222222222222222222222222222222222222222222
1111111111111222222222222222223333333333333333333332222222222222222222222222222
1111111111122222222222233333333333333333333333333333333333222222222222222222222
1111111111222222222333333333333333333333334444456655444433333322222222222222222
1111111122222222333333333333333333333344444445562116654444443333322222222222222
1111111222222333333333333333333333344444444555562332626544444433333322222222222
1111112222233333333333333333333344444444455556123555521655444444333333222222222
1111122223333333333333333333334444444445556662666   651165555444433333332222222
11112223333333333333333333344444444455666611122      14216665555443333333322222
111122333333333333333333344444445555145243342614     36542211121654433333332222
111223333333333333333344444555555566152  46               352646365433333333222
1122333333333333333444555555555666622411                       4165543333333322
112333333333333444566666555566661135 6                        34165544333333332
123333333444444555622211124211112253                           5331544433333333
123334444444455556124133665111433453                            326544433333333
13344444444555556112413        2122                              36544443333333
144444444555566253351            2                              415544443333333
145555566666112354135                                          2165544443333333
245555566666112354135                                          2165544443333333
144444444555566253351            2                              415544443333333
13344444444555556112413        2122                              36544443333333 
123334444444455556124133665111433453                            326544433333333
123333333444444555622211124211112253                           5321544433333333
112333333333333444566666555566661135 6                        34165544333333332
1122333333333333333444555555555666622411                       4165543333333322
111223333333333333333344444555555566152  46               352646365433333333222
111122333333333333333333344444445555145243342614     36542211121654433333332222
11112223333333333333333333344444444455666611122      14216665555443333333322222
1111122223333333333333333333334444444445556662666   151165555444433333332222222
1111112222233333333333333333333344444444455556123555521655444444333333222222222
1111111222222333333333333333333333344444444555562332626544444433333322222222222
1111111122222222333333333333333333333344444445562116654444443333322222222222222
1111111111222222222333333333333333333333334444456655444433333322222222222222222
1111111111122222222222233333333333333333333333333333333333222222222222222222222
1111111111111222222222222222223333333333333333333332222222222222222222222222222
1111111111111122222222222222222222222222222222222222222222222222222222222222222
1111111111111111222222222222222222222222222222222222222222222222222222222222222
1111111111111111111222222222222222222222222222222222222222222222222222222222222
1111111111111111111112222222222222222222222222222222222222222222222222222222222

OK, here is my attempt to explain the code. For a deeper description of the Mandelbrot set see e.g. lazyr's write-up here.

Here is the code again, with some whitespace and line numbers (commented out):

/* 0 */ #include <stdio.h>
 
/* 1 */ float o=0.075, h=1.5, T, r, O, l, I;
/* 2 */ int _, L=80, s=3200;

/* 3 */ main(){

/* 4 */   for( ;
/* 5 */        s%L || (h-=o,T= -2),s;
/* 6 */        4 -(r=O*O)<(l=I*I) | ++_==L && 
/* 7 */ 	 write(1,(--s%L?_<L?--_ %6:6:7)+"123456 \n",1) 
/* 8 */ 	 && (O=I=l=_=r=0,T+=o /2)
/* 9 */      )
/*10 */     O=I*2*O+h,I=l+T-r;

/*11 */ }
  1. Line 0 imports the standard input/output code needed to call write.
  2. Lines 1 and 2 declare the real valued and integer valued variables, respectively.
  3. Line 4 begins a for loop. The usual format for such a statement is for(i=0; i<n; i++), where the first third is an initialization, the second a condition, and the third is an operation performed each iteration. Thus, here the coder is combining many steps into these slots. No variables are being initialized in the loop, thus the empty space before the semi-colon in line 4.
  4. Line 5 has the condition which, if satisfied, has the code execute the contents of the for loop (here line 10). The condition is true if s is not equal to zero. Note that if s is a multiple of L, h gets decreased by o and T is set equal to -2.
  5. Line 6 does many things. Let's start with the smallest parts first. In parentheses we see r=O*O and l=I*I; these are assignments of the variables r and l. Using OR (|) and AND (&&) is a sneaky way of doing many things in one statement. Every part of an AND statement gets executed in order until a false condition is found, while every part of an OR statement is executed until a true condition is found. Thus, if and only if 4 - r < l then _ is not incremented, and both lines 7 and 8 are executed; otherwise _ is incremented, and only if it is then equal to L do lines 7 and 8 get executed.
  6. Unfortunately, I've never seen write used in C before, so I will have to look up the syntax. I think this is where the meat of the Mandelbrot set math is being done. It decides what number to print based on the algorithm (see Mandelbrot set) and does it.
  7. Line 8 resets a bunch of variables to zero, and adds o/2 to T.
  8. Line 9 closes the argument of the for statement.
  9. Line 10 contains the contents of the for loop.
Stay tuned, still working...

The Mandelbrot Set is calculated by repeating:
z = z^2 + c;
until the absolute value of z (distance from 0) is greater than say 4, where z and c are complex numbers, c is the coordinates of the point being calculated.

Let z be a + bi and c = e + fi, the calculation becomes:
z = (a+bi)^2 + (e+fi)
z = a^2 + 2abi - b^2 + e + fi
or
a = a^2 - b^2 + e
b = 2ab + f

The test condition,
a^2 + b^2 > 4
is changed here to the equivalent
a^2 > 4 - b^2
in this code.

Replacing all the sneaky code with more traditional structures, we get:

float o=0.075; /* distance between pixels     */
float h=1.5;   /* imag component of c         */
float T;       /* real component of c         */
float r;       /* O squared                   */
float O;       /* imag component of z         */
float l;       /* I squared                   */
float I;       /* real component of z         */
int _;         /* iteration count             */
int L=80       /* max iterations & line width */
int s=3200;    /* total "pixels": 80*40       */

main(){
    while (s>0) {  /*  lines 4 & 6 */
        r = O * O;
        l = I * I;

        _++;

        if (4-r<l || _==L) {
/* line 6 above ends here, next is line 7 */
            s--; _--;
            if (s%L==0) {
                write(1,"\n",1);
            } else {
                if (_<L) {
                    write(1,_%6+"123456",1);
                } else {
                    write(1," ",1);
                }
            }
        /* line 8: */
            O = I = l = _ = r = 0;
            T += o/2;
        }

    /* line 10: z=z^2+c */
        O = I * 2 * O + h;
        I = l + T - r;

/* this next bit is line 5 */
        if (s%L) {
            h -= o;
            T = -2;
        }
    }
}
The main loop has been sucked into the iteration loop, updating its variables when each pixel is finished.

Log in or register to write something here or to contact authors.