Sunday Times Teaser 2556 – Dotty Squares

by Andrew Skidmore

This is an interesting teaser as it is easy to miss the fact that there are squares whose sides are not parallel to the sides of the grid. After some effort it is possible to work out that the total number of squares for a grid of $$n$$ by $$n$$ dots is given by $$n^2(n^2-1)/12$$, which generates the sequence 0, 1, 6, 20, 50, 105, 196, … as given here in the The On-Line Encyclopedia of Integer Sequences (the count for rectangles rather than squares is given here). When this teaser was first published, I wrote one of my first Python programs to count the number of squares and rectangles in a square grid of dots. I have just dug it out and improved it a bit so here it is: