In an idle moment a while ago I wrote a program to generate "factorization diagrams". Here’s 700:

It’s easy to see (I hope), just by looking at the arrangement of dots, that there are in total.

Here’s how I did it. First, a few imports: a function to do factorization of integers, and a library to draw pictures (yes, this is the library I wrote myself; I promise to write more about it soon!).

> module Factorization where > > import Math . NumberTheory . Primes . Factorisation ( factorise ) > > import Diagrams . Prelude > import Diagrams . Backend . Cairo . CmdLine > > type Picture = Diagram Cairo R2

The primeLayout function takes an integer n (assumed to be a prime number) and some sort of picture, and symmetrically arranges n copies of the picture.

> primeLayout :: Integer -> Picture -> Picture

There is a special case for 2: if the picture is wider than tall, then we put the two copies one above the other; otherwise, we put them next to each other. In both cases we also add some space in between the copies (equal to half the height or width, respectively).

> primeLayout 2 d > | width d > height d = d === strutY ( height d / 2 ) === d > | otherwise = d ||| strutX ( width d / 2 ) ||| d

This means when there are multiple factors of two and we call primeLayout repeatedly, we end up with things like

If we always put the two copies (say) next to each other, we would get

which is much clunkier and harder to understand at a glance.

For other primes, we create a regular polygon of the appropriate size (using some trig I worked out on a napkin, don’t ask me to explain it) and position copies of the picture at the polygon’s vertices.

> primeLayout p d = decoratePath pts ( repeat d ) > where pts = polygon with { polyType = PolyRegular ( fromIntegral p ) r > , polyOrient = OrientH > } > w = max ( width d ) ( height d ) > r = w * c / sin ( tau / ( 2 * fromIntegral p ) ) > c = 0.75

For example, here’s primeLayout 5 applied to a green square:

Now, given a list of prime factors, we recursively generate an entire picture as follows. First, if the list of prime factors is empty, that represents the number 1, so we just draw a black dot.

> factorDiagram' :: [ Integer ] -> Diagram Cairo R2 > factorDiagram' [] = circle 1 # fc black

Otherwise, if the first prime is called p and the rest are ps , we recursively generate a picture from the rest of the primes ps , and then lay out p copies of that picture using the primeLayout function.

> factorDiagram' ( p : ps ) = primeLayout p ( factorDiagram' ps ) # centerXY

Finally, to turn a number into its factorization diagram, we factorize it, normalize the returned factorization into a list of primes, reverse it so the bigger primes come first, and call factorDiagram' .

> factorDiagram :: Integer -> Diagram Cairo R2 > factorDiagram = factorDiagram' > . reverse > . concatMap ( uncurry $ flip replicate ) > . factorise

And voila! Of course, this really only works well for numbers with prime factors drawn from the set (and perhaps ). For example, here’s 121:

Are there 11 dots in those circles? 13? I can’t really tell at a glance. And here’s 611:

Uhh… well, at least it’s pretty!

Here are the factorization diagrams for all the integers from 1 to 36:

Powers of three are especially fun, since their factorization diagrams are Sierpinski triangles! For example, here’s :

Powers of two are also fun. Here’s :

[ETA: as anon points out, this fractal has a name too: Cantor dust!]

One last one: 104.

I wish I knew how to make a website where you could enter a number and have it show you the factorization diagram… maybe eventually.

(In case you were wondering, .)