Mandelbrot Set ...in 134 Characters
Code golf is not generally the best use of Ruby, but when I saw my Mandelbrot fractal generator was less than 400 bytes, I decided to squeeze the program down into a single tweet.
Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!
Code golf is not generally the best use of Ruby, but when I saw my Mandelbrot fractal generator was less than 400 bytes, I decided to squeeze the program down into a single tweet.
Comments
Cool, I didn’t know about []*’’ before (thanks); a couple of these changes could be considered “cheating” (-:
80.times{|a|p (0..300).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/150.0-1.5,2*x*y+a/40.0-1,i+1)until(x*x+y*y>4||i>98);i>98?0:1}*''}
p! OH man! I had totally forgotten about p! (I did think about pp, but that necessitates a require “pp” in the code.)
Your output is not identical to mine, but this is code golf we’re talking about here. Your output is clearly visible as a mandelbrot set. Clocking in at 120 chars… daaang. My hat is off to you, sir or madam.
May I quote your code on my blog?
Oh wow–I just shaved another 2 characters off. Instead of i>98?0:1, try 99<=>i. :-)
For purposes of Twitter, it’s actually one character longer, though: the added < is actually “<”.
My tip is to turn off syntax highlighting if you are going to golf :)
Your original and the above have () brackets that can be removed and replaced with a space between the keyword and the statement saving one stroke each.
124 same output, same algorithm, Ruby 1.9:
60.times{|a|p (0..240).map{|b|x=y=i=0;x,y,i=x*x-y*y+b/120.0-1.5,2*x*y+a/30.0-1,i+1 until x*x+y*y>4||i==99;i==99?'#':'.'}*''}
Certainly, feel free. :-)
Post a comment