How many squares in a grid of 17×17 points?
Find the general formula of n×n points and apply it for n=17.
If you google this question chances are big that you will find 1496 which is not the right answer.
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.
N o t e : This is not my solution. You can check it out at here .
For the general case, let's assume that 2 ≤ n ≤ m .
Every possible square has an axis-aligned boundary box that is a square of edge length a . Such a square can fit into the n × m grid at
P ( a ) = ( m − a ) ( n − a )
positions, see illustration below (left). If we define each square by the edge of its north-west corner ( d x , d y ) , d x > 0 , d y ≥ 0 ; the edge length of the bounding box is:
a = d x + d y
There are a possibilities to split each a into d x and d y as shown below (right). (Note that d x = 0 , because such a square would be a 9 0 ° rotation of an axis-aligned square that has already been accounted for by the case where d y = 0 .)
Possible bounding-box positions and possible squares in bounding box.
Possible edge lengths of bounding boxes are 0 < a < n . Therefore, the number of squares that fit into a grid of n × m nodes is:
N = a = 1 ∑ n − 1 a ( m − a ) ( n − a ) = a = 1 ∑ n − 1 ( a 3 − ( m + n ) a 2 + m n a )
Using the well-known (or easily googleable) Faulhaber formulas , we get:
N = 4 1 ( n − 1 ) 2 n 2 − 6 1 n ( n − 1 ) ( 2 n − 1 ) ( m + n ) + 2 1 ( n − 1 ) n 2 m
and after a bit of rearranging and simplifying:
N ( n , m ) = 1 2 1 n ( n − 1 ) ( n + 1 ) ( 2 m − n )
My approach is different from (and perhaps less elegant than) Wen1now's, but it arrives at the same solution. There's a difference in nomenclature; k is the edge length of the grid, m , n are the numbers of pegs along the edge as in the question. If we set n = m = k + 1 , we get:
N ( k ) = 1 2 1 k ( k + 1 ) 2 ( k + 2 )