I've been recently fascinated by the beauty, symmetry and mathematical richness of the Apollonian gaskets.

Apollonian gasket of type -1_2_2

So I felt myself challenged to see if it was possible to generate one in Mathematica with a very short code, guided by the belief that "Beautiful math can often also be very simple".

The result I came up with is the following code:

Graphics[{Purple   ,Circle[],Disk@@@Flatten[Table[1/(k^2+2) {{(-1)^r (-k^2+1), -2 (-1)^j k},1}, {k,0,9}, {j,0,1}, {r,0,1}],2]}]

Not bad for a minimalistic code, also considering that I found a simpler/shorter translation of Descartes formula to be used in my case.

Above code was short enough (124 characters) to be accepted and published as a tweet by Tweet-a-Program ‏@wolframtap (here's the link).

Partial Apollonian gasket

Anyway the Apollonian gasket produced by the short code quoted above is not complete, as it only produces the circles tangent to the outer circle and not the inner ones.

It's surely possible to realize some longer code to produce a full Apollonian gasket (with a parameter to set the limit for the number of iterations) drawing also the inner circles. There are good examples of full programs already done by others (like this)

But my question is whether such program can be reduced to a minimum length (I suppose it could be done with about 500 characters or less).

Amongst all the possible variations I'm interested on the basic Apollonian gasket generated by the outer circle (curvature -1) and 2 congruent inner circles with curvature 2 (model -1, 2, 2 as in the image above). That initial condition could generate, in sequence, the other circles with curvatures 3, 6, 15, 11, 14, 23, 38, 35.... using the Descartes' theorem formula.

My guess is that a very smart program (probably based on a Nest command) could be used in a very condensed code, also taking advantage of the symmetries of the gasket.

Any suggestion for the shortest possible code?

  • 7
    $\begingroup$ 289 characters codegolf.stackexchange.com/a/38652/315 $\endgroup$ Apr 7, 2015 at 20:50
  • $\begingroup$ @belisarius Wow! $\endgroup$
    – Luca M
    Apr 7, 2015 at 20:56
  • $\begingroup$ @belisarius I'm studying your code. I must say it's impressive and a very good example for learning the use of abbreviated code symbols (like ~, @@, ##2 etc.). I think you should post it here as an answer to my question as it could also help many Mathematica users trying to understand advanced programming. Furthermore there seems to be some error message running the code in Mathematica 10.1 that could be discussed here. $\endgroup$
    – Luca M
    Apr 8, 2015 at 13:50
  • 3
    $\begingroup$ Code Golf answers use convoluted programming tricks to reduce code size. They aren't GOOD examples and usually go against good programming practices. I'd never program like that if I need to maintain the code afterwards. In fact, I'm not sure of being able to read that code now:) $\endgroup$ Apr 8, 2015 at 13:56
  • $\begingroup$ That's sort of a relief for me. I found it very hard and I was beginning to thing I had become dumb and dimwitted. Anyway I'm trying to translate it in a more understandable form. $\endgroup$
    – Luca M
    Apr 8, 2015 at 14:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.