How many squares on a grid

How many unique squares can be drawn inside a 16 x 16 square if their vertices must lie on a grid (lattice) of integer points? The sides of these smaller squares need not be parallel to the sides of the large square. Any vertex or edge on the boundary of the large square is counted as inside the large square. Bonus: can you generalize this to an n x n square?

Note: This is not a packing problem. These squares can overlap. How many such squares are there?


The answer is 6936.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Chris Lewis
Aug 3, 2020

Say the total number of squares we can draw on an n × n n \times n grid is f ( n ) f(n) .

Start drawing a square from a point on the grid A ( p , q ) A(p,q) . Draw the next vertex at B ( p + u , q + v ) B(p+u,q+v) . This defines the remaining vertices; the full set is A ( p , q ) B ( p + u , q + v ) C ( p + u v , q + v u ) D ( p v , q u ) \begin{aligned} &A(p,q) \\&B(p+u,q+v)\\&C(p+u-v,q+v-u)\\&D(p-v,q-u) \end{aligned}

To make sure it fits on the grid, we need 0 p v , p + u n 0 q u , q + v n \begin{aligned} 0\le p-v, &\;\;\; p+u \le n \\ 0\le q-u, &\;\;\; q+v \le n \end{aligned}

This gives n + 1 u v n+1-u-v possible values of p p and the same for q q . For a fixed pair ( u , v ) (u,v) satisfying u + v 0 n u+v 0\le n , this means there are a total of ( n + 1 u v ) 2 (n+1-u-v)^2 possible starting points ( p , q ) (p,q) so that all vertices are on the grid.

In order to avoid double-counting we just need to insist that u 0 u \ge 0 and v 1 v \ge 1 (this avoids counting squares with sides on the gridlines twice).

This gives the formula f ( n ) = u = 0 n 1 v = 1 n u ( n + 1 u v ) 2 f(n)=\sum_{u=0}^{n-1}\sum_{v=1}^{n-u} (n+1-u-v)^2

...which looks pretty horrendous, but after doing the paperwork** we get the surprisingly neat f ( n ) = 1 12 n ( n + 1 ) 2 ( n + 2 ) f(n)=\frac{1}{12}n(n+1)^2 (n+2)

In particular, f ( 16 ) = 6936 f(16)=\boxed{6936} .


**There are various ways to work out the sum, such as:

  • Expand, and use the formulas for k \sum k and k 2 \sum k^2 repeatedly

  • Use Wolfram|Alpha to help

  • Recognise it will be a polynomial in n n , calculate some values and use a difference table to work out coefficients

I suspect there's a clever way to use symmetry here too, but perhaps that's a bonus question?

Excellent analysis!

Fletcher Mattox - 10 months, 1 week ago

The bonus question was the polynomial. When I discovered it (with a difference table), I realized this would make a good Brilliant problem.

Fletcher Mattox - 10 months, 1 week ago

Looking at it afresh, it's very similar in form to a binomial coefficient; in fact f ( n ) = 2 ( n + 3 4 ) ( n + 2 3 ) f(n)=2\binom{n+3}{4}-\binom{n+2}{3}

which suggests a combinatorial approach. (This makes sense - we're counting, after all.) Of course, there are different ways to put binomial coefficients together to get the same thing; but perhaps one of them will lead to an intuitive derivation.

Anyone up for a bonus bonus question?

Chris Lewis - 10 months, 1 week ago

Log in to reply

Of course!

Fletcher Mattox - 10 months, 1 week ago

It's also http://oeis.org/A002415

Fletcher Mattox - 10 months, 1 week ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...