# How to plot a Circles-and-Squares fractal

The Circles-and-Squares fractal is produced by iteration of the equation

$\quad \quad z_{n+1}=z_n^2\ ({\rm mod}\; m)$

which results in a Moiré-like pattern:

Source: Wolfram MathWorld

In another place I found the same pattern, but the equation said ${\rm modulo}\ n$ instead of $m$. Either way, this equation is very similar to the one generating the Mandelbrot set. How can it be plotted?

• No doubt there are more concise ways but try With[{r = Range[-50, 50]^2}, Image@Rescale@Mod[Outer[Plus, r, r], 100]] – Simon Woods Sep 19 '14 at 13:37
• In this way we get a low-res picture that's not scalable. The result should be plot or other similar constructs that are vectorial and can be exported as such (like in *.svg). – Bo C. Sep 19 '14 at 14:12
• @BoC. you can double the numbers and change Image to ArrayPlot in Simon's code to get a plot and higher resolution. E.g. With[{r = Range[-100, 100]^2}, ArrayPlot@(1 - Rescale@Mod[Outer[Plus, r, r], 200])] – kglr Sep 19 '14 at 17:57
• You should state your requirements in the question. I doubt you'll get a vectorial representation of a fractal image, though, unless it is something procedurally generated (e.g. by an L-System or such). – Yves Klett Sep 20 '14 at 7:21

The image taken from MathWorld appears to be something like this:

Image @ Rescale @ Table[Mod[x^2 + y^2, 100], {x, -50, 50}, {y, -50, 50}]


In my opinion this is not a fractal, and it certainly isn't produced by iteration. You described it as "Moiré-like", which is a far better description. It is simply a uniformly sampled 2D parabola, modulo some number. The pattern arises from the interaction between the fixed frequency of the sampling and the increasing frequency of the underlying function.

It's clearer in 1D:

Plot[Mod[x^2, 100], {x, -50, 50}, Exclusions -> None, PlotPoints -> 200,
PlotStyle -> Opacity[0.5], AspectRatio -> 0.3,
Epilog -> {PointSize[Medium], Point[{#, Mod[#^2, 100]} & /@ Range[-50, 50]]}]


So I don't really understand your comment about it being low-res and wanting a vectorial representation. The pattern appears because of the finite spatial sampling. Zooming into the function is very boring - you will just see the underlying wrapped parabola, not the never ending detail that a fractal provides:

## Something prettier

Ignoring the picture in the question, and looking at the iteration $z_{n+1}=z_n^2\ ({\rm mod}\; m)$ gives something much nicer (in my opinion). Here it is with $m=2$ and 1 to 4 iterations:

z0 = Table[x + I y, {x, -3.001, 3, 0.01}, {y, -3.002, 3, 0.01}];
results = Rest @ NestList[Mod[#^2, 2] &, z0, 4];
GraphicsGrid[Partition[Image @ Rescale @ Abs @ # & /@ results, 2]]


• That's an answer. (Crocodile Dundee voice) – Mr.Wizard Sep 20 '14 at 12:06
• That is something prettier. 1970s version: WatershedComponents[Image@Rescale@Abs@results[[4]]] // Colorize (better if resolution is doubled). – Michael E2 Sep 20 '14 at 12:15
• @MichaelE2, love it! Try this one: {b,h}=Rescale@#@results[[3]]&/@{Abs,Arg}; Image[{h,1-0b,1-b},Interleaving->False,ColorSpace->"HSB"] – Simon Woods Sep 20 '14 at 12:35
• How about only one set of concentric circles? Which part of the equation defines the ratios of successive circles' radius? – Bo C. Sep 20 '14 at 17:18
• @SimonWoods Oh that hurts my eyes! – Michael E2 Sep 20 '14 at 19:18