How many squares are there in the perimeter of a square?

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

This image shows the n = 4 case of the problem, and I'm not really sure of a better way to describe it. What we want is to count the red squares (i.e. the ones in the perimeter) for arbitrarily sized squares-of-squares.

So, I'm going to show two ways to solve this problem, both of which are reasonably intuitive, but the fact they're equivalent can provide insight into the structure of higher dimensional shapes. Let's call the number of red squares "p", for "perimeter".

The first way to solve the problem is like this. We can see that the square has 4 sides of side length n, which each contain n red squares, so we start with 4n. However, we can see above that there are only 12 red squares, not 16, when n = 4. This is because when we counted 4 sides worth of squares, we counted the corners twice, since each corner square is part of two edges. To correct for counting the corners twice, we look at how many there are (there are 4), and subtract that from 4n, giving us our first formula: p = 4n − 4.

With this formula we'd expect that when n = 6, we'd have 20 red squares, which as you can see, we do:

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 there are n2 squares in total, and (n − 2)2 black squares. Since all the squares which aren't black are red, we can find the amount of red squares by subtracting the number of black squares from the number of squares in total. This gives us our second formula: p = n2 − (n − 2)2.

These formulas are equivalent: n2 − (n − 2)2 is equal to n2 − (n2 − 4n + 4) by the binomial theorem, and then it simplifies to 4n − 4.


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. So we have 6 n×n squares-of-squares we need to count, so we start our formula with 6n2. Similarly, we've counted the edges twice, so we need to subtract 12 n-length edges from our total, so now we have 6n2 − 12. We're still not done though, since when we counted the faces, we counted the corners 3 times (since there are 3 faces per corner), and when we corrected for the edges, we undid this (since there are also 3 edges per corner), which means we essentially haven't counted the corners yet, so we need to add 8 to our total, which gives us the final formula p = 6n2 − 12n + 8.

6 faces, 12 corners, 8 edges. Bleh.

This is getting more complicated. Luckily, the parallel for the second formula doesn't get much harder, we just cube our terms instead of squaring them. i.e. we have p = n3 − (n − 2)3, for the same reason as it worked for squares. When you use the binomial theorem on (n − 2)3, you get n3 − 6n2 + 12n − 8, which means this formula simplifies to 6n2 − 12n + 8, which is our 6 faces, 12 corners, and 8 edges.


So, what about the 4 dimensional case? A tesseract has... I think 8 cubic "faces" (apparently they're called cells)? And... some amount of edges and corners, I guess?

I'm having trouble visualising this. Let's just skip the geometric formula and go straight onto p = n4 − (n − 2)4. With the same method as before, this is equal to n4 − (n4 − 8 n3 + 24n2 − 32n + 16), which simplifies to 8n3 − 24n2 + 32n − 16, which corresponds to our 8 cells and— oh, there are 24 faces, 32 edges, and 16 corners on a tesseract! We can just read the numbers from the formula, because even though we didn't start with the geometric formula which was based on information about the shape, we can still retrieve that information by expanding out the second 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 9-cubes are:

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