The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Shortest program that draws the Mandelbrot set

Last post 04-13-2007 1:35 PM by mbessey. 22 replies.
Page 1 of 1 (23 items)
Sort Posts: Previous Next
  • 04-08-2007 4:50 AM

    Shortest program that draws the Mandelbrot set

    The challenge is simple: create the smallest program you can in your language(s) of choice that draws the Mandelbrot set.

    The rules:

    • Program size will be measured by the size of the source, in bytes, with Unix-style line endings (i.e., LF only, and on every line, including the last)
    • You may output any graphics format or text, to stdout or a file; however, obscure formats few are likely to be able to view are discouraged
    • You must actually compute the values; using pre-generated data in any form is not allowed
    • The image must have an area of at least 1002
    • The image shouldn't have a ridiculous aspect ratio; there's no hard limit, but 2:1 in either direction is about as far as you should go, unless you have a good reason
    • The image must show the entire set, and shouldn't show too much outside of the set (if only a few pixels or characters of the image is the set, it's too much)

    My entry, in Python (155 bytes):

    w,h,r,i=200,100,range,lambda c:"# "[abs(reduce(lambda z,_:z*z+c,r(h)))<=2]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))
  • 04-08-2007 11:37 AM In reply to

    Re: Shortest program that draws the Mandelbrot set

    I don't think it can get much shorter than yours ;)

     

    For your interest, here's another short method to generate a fractal (Sierpinski's triangle). Perhaps it's well known, but (unrolled for clarity):

    for x in range(50):
        for y in range(50):
            if x & y:
                print "H",
            else:
                print " ",
        print

  • 04-08-2007 12:11 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    Well, there's always other languages (if it wasn't obvious, I meant this to be on a per-language basis). I'm sure someone will come along soon enough with a Perl version with a third of the size and a thousandth of the readability, anyway. I'd be pretty impressed if someone did manage to outdo me in Python though, and perhaps a little bit scared.


     

  • 04-08-2007 1:17 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

     

    I'd be pretty impressed if someone did manage to outdo me in Python though, and perhaps a little bit scared.

    There you go :>:

     

    w,h,r,i=110,99,range,lambda c:"# "[abs(reduce(lambda z,_:z*z+c,r(h)))<=2]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))

     

  • 04-08-2007 1:44 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    jergosh:

     

    I'd be pretty impressed if someone did manage to outdo me in Python though, and perhaps a little bit scared.

    There you go :>:

     

    w,h,r,i=110,99,range,lambda c:"# "[abs(reduce(lambda z,_:z*z+c,r(h)))<=2]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))
    Saving another precious byte:

     
    w,h,r,i=110,99,range,lambda c:"# "[2>abs(reduce(lambda z,_:z*z+c,r(h)))]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))
  • 04-08-2007 1:52 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    Autonuke:
    Saving another precious byte:

    w,h,r,i=110,99,range,lambda c:"# "[2>abs(reduce(lambda z,_:z*z+c,r(h)))]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))

    I don't quite see how 2>n differs from n<2 (which in turn is something else then n<=2) .

  • 04-08-2007 4:16 PM In reply to

    • iwpg
    • Top 500 Contributor
    • Joined on 05-24-2006
    • Posts 258

    Re: Shortest program that draws the Mandelbrot set

    OB Haskell:

    import Complex
    m z=if magnitude(foldr($)z(replicate 99((+z).(^2))))<=2 then ' 'else '#'
    main=mapM putStrLn[[m((x:+y)/40)|x<-[-98..55]]|y<-[-55..55]]

    149 characters - 15 of which are for importing the Complex module, which is kinda unfortunate, but oh well.

  • 04-08-2007 5:06 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    jergosh:

    There you go :>:

    w,h,r,i=110,99,range,lambda c:"# "[abs(reduce(lambda z,_:z*z+c,r(h)))<=2]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))


    Changing the height and width is hardly outdoing me. I'm not very scared

    Autonuke:

    w,h,r,i=110,99,range,lambda c:"# "[2>abs(reduce(lambda z,_:z*z+c,r(h)))]
    print"\n".join("".join(i((x-w/2)*4./w+(y-h/2)*4j/h)for x in r(w))for y in r(h))

    2>|z| = |z|<2, however the points in the Mandelbrot set within the radius of 2, and hence it must be |z|≤2. You probably aren't going to shave off any characters I couldn't have without delving deep into the internals of Python.
  • 04-08-2007 6:39 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    Down to 124 bytes:

    r=range
    print"\n".join("".join("# "[abs(reduce(lambda z,_:z*z+x/48.+y/24j,r(99)))<=2]for x in r(-96,97))for y in r(-48,49))
  • 04-08-2007 7:02 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    Changing the height and width is hardly outdoing me. I'm not very scared

    So sorry, thought you wanted it shorter. And, yes it is, in this case.

    2>|z| = |z|<2, however the points in the Mandelbrot set within the radius of 2, and hence it must be |z|≤2. You probably aren't going to shave off any characters I couldn't have without delving deep into the internals of Python.

    Well... 

  • 04-08-2007 7:28 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    The Real WTF:

    The challenge is simple: create the smallest program you can in your language(s) of choice that draws the Mandelbrot set.

    Ok. This is my code:

    M

    I'll write the compiler for it later. Let's call this language Mandeldraw. Every M in input file causes drawing of Mandelbrot set. ;) There's no new line at the end.

    I'm planning to optimize it further...

  • 04-08-2007 8:22 PM In reply to

    • iwpg
    • Top 500 Contributor
    • Joined on 05-24-2006
    • Posts 258

    Re: Shortest program that draws the Mandelbrot set

    The Real WTF:

    Down to 124 bytes:

    I managed to get mine down to 128 - it turns out to be basically the same as yours, except for the language.  I did have something similar to the foldl/reduce earlier, but I replaced it with something shorter, and didn't realise that I could make it shorter still by keeping that particular part.  I originally avoided the string indexing because I assumed the explicit conversion from Bool to Int would be too verbose, but it actually turned out to be shorter than what I had before. 

    import Complex
    main=mapM putStrLn[["# "!!fromEnum(magnitude(foldl(\t _->t^2+(x:+y)/40)0[0..99])<=2)|x<-[-98..55]]|y<-[-55..55]]
  • 04-09-2007 12:03 PM In reply to

    • kirchhoff
    • Top 500 Contributor
    • Joined on 02-27-2007
    • ECE 280 (Circuit Analysis)
    • Posts 217

    Re: Shortest program that draws the Mandelbrot set

    print (-55..55).map{|y|(-98..55).map{|x|(0..99).inject{|z,r|z*z+Complex(x,y)/40.0}.abs<2?"#":" "}.join}.join("\n")

    114 characters; to be executed with ruby -rcomplex -e. If the require'complex'; is required for the rules, then 132 characters.

    Currently no easy way to get rid of the print; irb doesn't interpret control characters when printing implictly returned strings. 

  • 04-11-2007 6:57 AM In reply to

    • Mek
    • Not Ranked
    • Joined on 12-15-2004
    • Posts 8

    Re: Shortest program that draws the Mandelbrot set

    straight forward C implementation, 191 bytes (all on one line without line ending):

    i;main(){float X,Y=-1,x,y,x2,y2;for(;Y<1;putchar(10),Y+=.02)for(X=-2;X<1;X+=.02){x=X;y=Y;x2=x*x;y2=y*y;for(i=0;i<999&&x2+y2<4;i++){y=2*x*y+Y;x=x2-y2+X;x2=x*x;y2=y*y;}putchar(i>998?'X':'.');}} 

  • 04-11-2007 7:06 AM In reply to

    • Mek
    • Not Ranked
    • Joined on 12-15-2004
    • Posts 8

    Re: Shortest program that draws the Mandelbrot set

    simple improvements, 176 bytes:

    i;main(){float X,Y=-1,x,y,a,b;for(;Y<1;putchar(10),Y+=.02)for(X=-2;X<1;X+=.02){x=X;y=Y;a=x*x;b=y*y;for(i=0;i<999&&a+b<4;i++)y=2*x*y+Y,x=a-b+X,a=x*x,b=y*y;putchar(i/999*3+32);}}
     

  • 04-11-2007 7:15 AM In reply to

    • Mek
    • Not Ranked
    • Joined on 12-15-2004
    • Posts 8

    Re: Shortest program that draws the Mandelbrot set

    down to 161 bytes:

    i;main(){float X,Y=-1,x,y,a,b;for(;Y<1;putchar(10),Y+=.02)for(X=-2;x=X,y=Y,X<1;putchar(i/999*3+32),X+=.02)for(i=0;i++<998&&4>(a=x*x)+(b=y*y);)y=2*x*y+Y,x=a-b+X;} 

  • 04-11-2007 7:34 AM In reply to

    • Mek
    • Not Ranked
    • Joined on 12-15-2004
    • Posts 8

    Re: Shortest program that draws the Mandelbrot set

    down to 158 (with a little loss of precision):

    i;main(){float X,Y=-1,x,y,a,b;for(;Y<1;putchar(10),Y+=.02)for(X=-2;x=X,y=Y,X<1;putchar(i/99+32),X+=.02)for(i=0;i++<296&&4>(a=x*x)+(b=y*y);)y=2*x*y+Y,x=a-b+X;}
     

  • 04-11-2007 8:15 AM In reply to

    Re: Shortest program that draws the Mandelbrot set

    Sorry to interrupt, but I'd like to suggest that people post their solutions unobfuscated, and count chars as if they were.

    In complex analysis, a meromorphic function on an open subset D of the complex plane is a function that is holomorphic on all D except a set of isolated points

  • 04-11-2007 8:23 AM In reply to

    • Mek
    • Not Ranked
    • Joined on 12-15-2004
    • Posts 8

    Re: Shortest program that draws the Mandelbrot set

    Okay, my code with indentation:

    i;
    main() {
            float X, Y = -1, x, y, a, b;
            for (; Y < 1; putchar(10), Y += .02)
                    for (X = -2; x = X, y = Y, X < 1; putchar(i / 99 + 32), X += .02)
                            for (i = 0; i++ < 296 && 4 > (a = x * x) + (b = y * y);)
                                    y = 2 * x * y + Y, x = a - b + X;
    }
     

  • 04-11-2007 3:09 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    for($b=-2;$b<2;$b+=.1,print"\n"){
        for($a=-2;$a<2;$a+=.1,print(chr($i%99+32))){
            $x=$a;$y=$b;
            for($i=0;($c=$x**2)+($d=$y**2)<4and$i++<99;){
                $y=2*$x*$y+$b;
                $x=$c-$d+$a;
            }
        }
    }

     

    163, but perl needs all these stupid $s, and there are 25 of them , so I am comfortable saying  138 :-)

  • 04-11-2007 3:58 PM In reply to

    • iwpg
    • Top 500 Contributor
    • Joined on 05-24-2006
    • Posts 258

    Re: Shortest program that draws the Mandelbrot set

    dhromed:
    Sorry to interrupt, but I'd like to suggest that people post their solutions unobfuscated,

    Here's mine, with added whitespace and some trivial rearrangements to make the line-breaks go in nicer places:

    import Complex
    
    main = mapM putStrLn [["# " !! (fromEnum $ 2 >=
                                    (magnitude $ foldl (\t _ -> t^2 + (x:+y)/40)
                                                   0 [0..99]))
                           | x <- [-98..55]]
                          | y <- [-55..55]]
    
    

    dhromed:
    and count chars as if they were.

    Do you mean "as if they were obfuscated", i.e. the previous char count is still valid? 

  • 04-12-2007 10:46 AM In reply to

    Re: Shortest program that draws the Mandelbrot set

    iwpg:

    dhromed:
    and count chars as if they were.

    Do you mean "as if they were obfuscated", i.e. the previous char count is still valid? 

    *ahem* 

    Yes.

    Cursed double negatives!


    In complex analysis, a meromorphic function on an open subset D of the complex plane is a function that is holomorphic on all D except a set of isolated points

  • 04-13-2007 1:35 PM In reply to

    Re: Shortest program that draws the Mandelbrot set

    I managed to remove most of the $ from coplate's entry, but the RE machinery needed to do that takes up nearly as much space:
    $_='~B"\n"){~A(chr(I%99+32))){X=A;Y=B;for(I=0;(C=X**2)+(D=Y**2)<4andI++<99;){Y=2*X*Y+B;X=C-D+A;}}}';
    s/~(.)/for\($1=-2;$1<2;$1+=\.1,print/g;
    s/([A-Z])/\$$1/g;
    eval;
    161 bytes. I'm not sure that was actually worth the effort...
    (that IS the unobfuscated version)
Page 1 of 1 (23 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems