Counting Squares with Mathematical Tricks

Squares of Squares

How many squares are there in the perimeter of a square of squares? It's probably a little hard to describe in words, so here's a picture:

a 4 by 4 grid of squares, with the outer 12 squares highlighted red

We want to count the red squares, i.e. the ones in the perimeter. Here we have a "square of squares" of size 4×4, (let's call this n = 4), and there are 12 red squares. What we want is a formula to turn n into the amount of red squares, for any size.

So, I'm going to show two ways to solve this problem, both of which are reasonably intuitive, but the fact they're equivalent is interesting, and can be used to reveal properties of higher-dimensional shapes.

The first way to solve the problem starts with the fact that a square has 4 sides. Since the red squares are all in the sides, and all the sides are n squares long, we can multiply n by 4. With the example above, this gives us 16. This isn't the correct answer, but it's close — it's off by 4, one for each of the corners. The red squares in the corners belong to two edges each, so simply multiplying n by 4 counts them twice. This is easy to fix, since we know how many corners there are, so we can we can account for this surplus in our formula, which says that number of red squares in the perimeter of a square of squares of size n is 4n − 4.

With this formula we'd expect that when n = 6, we'd have 20 red squares, which you can count for yourself:

a 6 by 6 grid of squares, with the outer 20 squares highlighted red

The second way to solve the problem is to notice that the black squares in the middle also form a square. In the first image, it's a 2×2 square, and in the larger one it's a 4×4 square. All the squares are either black or red, so if we know the total amount of squares, and the number of black squares, we can calculate the number of red squares. In the first image, there are 42 = 16 squares in total, and 22 = 4 black ones. 16 − 4 = 12, which is the result we expected.

So, we have n2 squares in total (that's what "squared" means after all) and (n − 2)2 black squares. We can subtract these from the total, giving us our second formula, which says that the number of red squares is n2 − (n − 2)2.

It seems a little more complicated, but this is equivalent to the first formula. We can show this using the binomial theorem, in particular the fact that (x + y)2 = x2 + 2xy + y2, which means that (n − 2)2 is equal to n2 − 4n + 4. This means we can take our second formula, n2 − (n − 2)2, and substitute n2 − 4n + 4 in the place of (n − 2)2, leaving us with n2 − (n2 − 4n + 4), which simplifies to 4n − 4, the first formula.

The Third Dimension

So, let's try and generalise these formulas to higher dimensions. What's the version of this for a cube? Unfortunately my artistic expertise ends in the second dimension, so from here on out it's going to be more abstract and wordy, but it will still be based on visual intuition for now.

Cubes have 6 faces, 12 edges, and 8 corners. This means we have 6 square faces that we need to count, so we start our formula with 6n2. Like before, this gives the wrong answer, because we count edges and corners multiple times. Two faces join at each edge, so we've counted each edge twice, and we need to account for that. We subtract the 12 n-length edges from the count, so now our formula is 6n2 − 12n.

We've also counted the corners multiple times. Counting the faces like we did meant that we overshot, since 3 faces join at each corner, but subtracting the edges undid that, since 3 edges join at each corner as well. In This means the corners aren't included yet, and since there are 8 of them, we can finish our formula: 6n2 − 12n + 8.

This corresponds to the cube's 6 edges, 12 edges, and 8 corners.

This is a lot more work than we had to do for squares. Luckily, the parallel for the second formula doesn't get much harder, we just cube our terms instead of squaring them. Subtracting the inner cube from the outer cube looks like n3 - (n - 2)3, and our work here is done. To check it's equivalent to the first formula, we can use the next iteration of the binomial theorem, which says that (x + y)3 = x3 + 3x2y + 3xy2 + y3. When you use this on (n - 2)3, you get n3 − 6n2 + 12n − 8, which means our overall formula simplifies to 6n2 − 12n + 8, which is what we got from the first method.

The Fourth Dimension

So, what about the 4 dimensional case? A tesseract is made of cubes (or "cells"), in the same way a cube is made of squares, but it's not obvious how many. You could use a visual aid like a 3d embedding, but making the 3d embedding would require you to know how many corners there were in the first place, and so on.

Let's skip the geometric way to find a formula, and start with n4 - (n - 2)4 as our formula this time, and let's expand it out. We can use the binomial theorem to find out that (x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4. This means that (n - 2)4 is n4 - 8n3 + 24n2 − 32n + 16, which means that another equivalent formula for how many tesseracts there are on the surface of a tesseract of tesseracts is 8n3 - 24n2 + 32n - 16.

There's something interesting about the 8n3 term. It might help to read it out loud as "eight n cubed", or maybe you've already realised what's going on. This term represents that a tesseract is made of 8 cubes. "24 squared" accounts for the 24 square faces, and likewise, there are 32 edges and 16 corners. We can just read the numbers off the formula, because even though we didn't start with geometric knowledge about the shape, the equivalence of the formulas encodes that information, which we've retrieved by expanding out the second more general type of formula.

And Beyond

And we can keep going with this method, no matter how hard it is to actually visualise a 5-, 6-, or 7-dimensional hypercube. The formulas for the 5- to 7-cubes are:

so now you know how many edges a 7-cube has.