cancel
Showing results for 
Search instead for 
Did you mean: 

Mandelbrot man rides again

SJT
Contributor III
Contributor III

In the forthcoming episode of the ArrayCast podcast our guest Michal Wallace of 1010data referred to an ad he had once placed on LinkedIn to recruit someone who would learn k3, an ancestor language of q. 

In the job ad he showed the k3 code to print an ASCII version of the Mandelbrot set, reasoning that it would filter for devs who would take to k3.

Screenshot 2022-11-10 at 11.29.18.png

The code looked so elegant that I translated it to q for your typing pleasure.

/ q example: Mandelbrot set
/ adapted from https://www.linkedin.com/pulse/want-work-k-1010data-michal-wallace
s:{(.[-]x*x),2*prd x}            / complex square (x is R,Im pair)
m:{floor sqrt sum x*x}           / magnitude (distance from origin)

d: 120 60                        / dimensions of the picture
t: -88 -30                       / camera translation
f: reciprocal 40 20              / scale factor

c: (,/:\:) . f * t + til each d  / complex plane near mandelbrot set
z: d # enlist 0 0                / 3d array of zeroes in same shape
mb: c+ (s'')@					 / Mandelbrot: s(z) + c
r: 1 _ 8 mb\z              		 / collect 8 times

o: " 12345678"@ sum 2<m'''[r]    / "color" by how soon point "escapes"

-1 "\n"sv flip o;                / transpose and print the output

 

1 ACCEPTED SOLUTION

cillianreilly
New Contributor III

Very cool. I had to see this in action, here's a quick "animation" of the set converging - I recommend full screen viewing. Hypnotic stuff.

{r: 1 _ x mb\y;o: " 12345678abcdef"@ sum 2<m'''[r];-1 "\n"sv flip o;system"sleep 1";}[;z]each 1+til 15;

 

View solution in original post

3 REPLIES 3

LeahS
Moderator Moderator
Moderator

Very nice @SJT 

Another great episode! 🙌

cillianreilly
New Contributor III

Very cool. I had to see this in action, here's a quick "animation" of the set converging - I recommend full screen viewing. Hypnotic stuff.

{r: 1 _ x mb\y;o: " 12345678abcdef"@ sum 2<m'''[r];-1 "\n"sv flip o;system"sleep 1";}[;z]each 1+til 15;

 

Oooooh – that is gorgeous!