
Shortest program that draws the Mandelbrot set
Last post 04132007 1:35 PM by mbessey. 22 replies.

04082007 4:50 AM



The Real WTF
 Joined on 08232006
 Posts 22

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 Unixstyle 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 pregenerated data in any form is not allowed
 The image must have an area of at least 100^{2}
 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((xw/2)*4./w+(yh/2)*4j/h)for x in r(w))for y in r(h))




jergosh
 Joined on 03132007
 Posts 19

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




The Real WTF
 Joined on 08232006
 Posts 22

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 perlanguage 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.




jergosh
 Joined on 03132007
 Posts 19

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((xw/2)*4./w+(yh/2)*4j/h)for x in r(w))for y in r(h))




Autonuke
 Joined on 11072006
 Posts 50

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((xw/2)*4./w+(yh/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((xw/2)*4./w+(yh/2)*4j/h)for x in r(w))for y in r(h))




jergosh
 Joined on 03132007
 Posts 19

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((xw/2)*4./w+(yh/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) .




iwpg
 Joined on 05242006
 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.




The Real WTF
 Joined on 08232006
 Posts 22

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((xw/2)*4./w+(yh/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((xw/2)*4./w+(yh/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.




The Real WTF
 Joined on 08232006
 Posts 22

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))




jergosh
 Joined on 03132007
 Posts 19

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...




viraptor
 Joined on 02182006
 U.K.
 Posts 320

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...




iwpg
 Joined on 05242006
 Posts 258

Re: Shortest program that draws the Mandelbrot set
The Real WTF:
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]]




kirchhoff
 Joined on 02272007
 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,rz*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.




Mek
 Joined on 12152004
 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=x2y2+X;x2=x*x;y2=y*y;}putchar(i>998?'X':'.');}}




Mek
 Joined on 12152004
 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=ab+X,a=x*x,b=y*y;putchar(i/999*3+32);}}




Mek
 Joined on 12152004
 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=ab+X;}




Mek
 Joined on 12152004
 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=ab+X;}





Mek
 Joined on 12152004
 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; }




coplate
 Joined on 12052006
 Posts 16

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 :)




iwpg
 Joined on 05242006
 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 linebreaks 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?




dhromed
 Joined on 04132005
 Dutchland
 Posts 11,909

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




mbessey
 Joined on 04052006
 Posts 51

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=CD+A;}}}'; s/~(.)/for\($1=2;$1<2;$1+=\.1,print/g; s/([AZ])/\$$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)


