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:

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 4`n` − 4.

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

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 4^{2} = 16 squares in total, and 2^{2} = 4 black ones. 16 − 4 = 12, which is the result we expected.

So, we have `n`^{2} 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 `n`^{2} − (`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} = `x`^{2} + 2`x``y` + `y`^{2}, which means that (`n` − 2)^{2} is equal to `n`^{2} − 4`n` + 4. This means we can take our second formula, `n`^{2} − (`n` − 2)^{2}, and substitute `n`^{2} − 4`n` + 4 in the place of (`n` − 2)^{2}, leaving us with `n`^{2} − (`n`^{2} − 4`n` + 4), which simplifies to 4`n` − 4, the first formula.

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 6`n`^{2}. 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 6`n`^{2} − 12`n`.

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: 6`n`^{2} − 12`n` + 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 `n`^{3} - (`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} = `x`^{3} + 3`x`^{2}`y` + 3`x``y`^{2} + `y`^{3}. When you use this on (`n` - 2)^{3}, you get `n`^{3} − 6`n`^{2} + 12`n` − 8, which means our overall formula simplifies to 6`n`^{2} − 12`n` + 8, which is what we got from the first method.

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 `n`^{4} - (`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} = `x`^{4} + 4`x`^{3}`y` + 6`x`^{2}`y`^{2} + 4`x``y`^{3} + `y`^{4}. This means that (`n` - 2)^{4} is `n`^{4} - 8`n`^{3} + 24`n`^{2} − 32`n` + 16, which means that another equivalent formula for how many tesseracts there are on the surface of a tesseract of tesseracts is 8`n`^{3} - 24`n`^{2} + 32`n` - 16.

There's something interesting about the 8`n`^{3} 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 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:

`n`^{5}− (`n`− 2)^{5}= 10`n`^{4}− 40`n`^{3}+ 80`n`^{2}− 80`n`+ 32`n`^{6}− (`n`− 2)^{6}= 12`n`^{5}− 60`n`^{4}+ 160`n`^{3}− 240`n`^{2}+ 192`n`− 64`n`^{7}− (`n`− 2)^{7}= 14`n`^{6}− 84`n`^{5}+ 280`n`^{4}− 560`n`^{3}+ 672`n`^{2}− 448`n`+ 128

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