(* Modification of Szabolcs' answer to http://stackoverflow.com/q/6631333/421225*) Clear[cantor, pairs, drawCantor] pairs[ch_] := pairs[ch] = Tuples[ch - 1, 2] cantor[size_, n_Integer, num_: 3, ch_: {1, 3}][pt_] := With[{s = size/num, ct = cantor[size/num, n - 1, num, ch]}, Table[ct[pt + tup s + 0.], {tup, pairs[ch]}]] cantor[size_, 0, _ : 3, _ : {1, 3}][pt_] := Rectangle[pt, pt + {size, size}] drawCantor[n_, num_: 3, ch_: {1, 3}] := Graphics[cantor[1, n, num, ch][{0, 0}]] Export["CantorDustAnim124.gif", Table[drawCantor[n, 4, {1, 2, 4}], {n, 1, 5}]] (* http://i.imgur.com/C6lVO.gif *)