# kasku.net/blog/2022/11/24

## Shapes

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

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:

The second way to solve the problem is to notice that there are n^{2}
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 = n^{2} − (n − 2)^{2}.

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

### 3D

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
6n^{2}. Similarly, we've counted the edges twice, so we need to subtract
12 n-length edges from our total, so now we have 6n^{2} − 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 = 6n^{2} − 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 = n^{3} − (n − 2)^{3}, for
the same reason as it worked for squares. When you use the binomial theorem on
(n − 2)^{3}, you get
n^{3} − 6n^{2} + 12n − 8,
which means this formula simplifies to
6n^{2} − 12n + 8, which is our 6 faces, 12 corners,
and 8 edges.

### 4D

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 = n^{4} − (n − 2)^{4}.
With the same method as before, this is equal to
n^{4} − (n^{4} − 8 n^{3} + 24n^{2} − 32n + 16), which simplifies to
8n^{3} − 24n^{2} + 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:

- n
^{5}− (n − 2)^{5}= 10n^{4}− 40n^{3}+ 80n^{2}− 80n + 32 - n
^{6}− (n − 2)^{6}= 12n^{5}− 60n^{4}+ 160n^{3}− 240n^{2}+ 192n − 64 - n
^{7}− (n − 2)^{7}= 14n^{6}− 84n^{5}+ 280n^{4}− 560n^{3}+ 672n^{2}− 448n + 128 - n
^{8}− (n − 2)^{8}= 16n^{7}− 112n^{6}+ 448n^{5}− 1120n^{4}+ 1792n^{3}− 1792n^{2}+ 1024n − 256 - n
^{9}− (n − 2)^{9}= 18n^{8}− 144n^{7}+ 672n^{6}− 2016n^{5}+ 4032n^{4}− 5376n^{3}+ 4608n^{2}− 2304n + 512

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