0

In 2 dimensions, there is a well-known puzzle: you have an $m \times n$ grid of squares of unit length. How many squares are in the grid? You need to count squares with edge legnth 1, 2, etc. There is a straightforward formula to find the answer.

In 3 dimensions, is there a similar formula? If you have an $x \times y \times z$ grid of $1\times1\times1$ cubes, how many cubes are there, including cubes of edge length 1, 2, etc. ?

I suspected this would be a simple formula, but I can't find this formula anywhere in my web searches!

I thought the formula would be a fun "outside the box" interpretation of a meme puzzle how many cubes are there on a trailer.

Presh
  • 1,861
  • 2
  • 18
  • 25

1 Answers1

0

I will be using the method given by Phira in the question you linked. First let us assume that $x \ge y \ge z$

  • Number of squares of size 1: $x*y*z$
  • Number of squares of size 2: $(x-1) * (y-1) * (z-1)$
  • ...
  • Number of squares of size $z$: $(x-z+1)*(y-z+1) $

We can add these together to get the total number of cubes, this sum can be written as: $$ f(x,y,z) = \sum_{i=0}^{z-1} (x-i)(y-i)(z-i) $$ However, I will re-index using $k=z-i$ and we get the sum: $$ f(x,y,z) = \sum_{k=1}^z k(x-z+k)(y-z+k) $$ Distributing, we get: $$ f(x,y,z) = \sum_{k=1}^z k^3 + k^2\big((y-z)+(x-z)\big) + k(x-z)(y-z) $$ Expressing this as 3 different sums each with a different degree of $k$, we get: $$ f(x,y,z) = \sum_{k=1}^z k^3 + (x+y-2z) \sum_{k=1}^z k^2 + (x-z)(y-z) \sum_{k=1}^z k$$ These are all known sums now so we can simplify this greatly. $$ f(x,y,z) = \frac{z^2(z+1)^2}{4} + (x+y-2z) \frac{z(z+1)(2z+1)}{6} + (x-z)(y-z)\frac{z(z+1)}{2} $$ As you can see this is a closed form solution. It is not pretty and the rest of my work will just be algebreaic manipulation to try to make it looks pretty. (You should check for mistakes youself, a lot of terms means a lot of places I could've made a mistake)

$$ f(x,y,z)= \frac{z(z+1)}{12}\left( 3z^2+3z + 2x+2y-4z + 4xz+4yx-8z^2 + 6xy - 6xz - 6yz +6z^2 \right) $$ $$ = \frac{z(z+1)}{12} \left( z^2 - z + 2x + 2y +6xy - 2xz - 2 yz \right) $$

$$ = \frac{z(z+1)xy}{2} + \frac{(z-1)z(z+1)}{12}(z-2x-2y) $$

Nic
  • 2,296
  • Thanks, I will start from your premises and work it out to verify the ultimate formula! – Presh Aug 31 '24 at 01:08