# How many squares are there?

I have a piece of graph paper, where the graph itself measures 18cm by 28cm, it is divided in lines, making a hue grid of tiny squares, each 1mm by 1mm, meaning there are $50400=180\times 280$ of the tiny squares.

However, there can be different sizes of square, up to 18cm side length. Including overlapping squares, how many are there in total?

I think that it has something to do with factorials and or powers, I could list out the amount of squares by size, but there has to be an easier way.

• The number of squares in a n×n grid is n(n+1)(2n+1)/6 – Angad May 3 '17 at 11:16
• I think it will be 5605 – Angad May 3 '17 at 11:18
• @Angad there are 50400 of the smallest squares... how are there 5606 in total – Cursed May 3 '17 at 11:21
• Wolfram Alpha gives this answer: wolframalpha.com/input/?i=50400(50400%2B1)(50400*2%2B1)%2F6 – Toby Mak May 3 '17 at 11:27
• Sorry that was a mistake I did not realised that the units were changed correct answer will be 5396350 – Angad May 3 '17 at 11:30

There are $180\times 280$ places you can put the bottom left corner of a 1mm square (anywhere except on the top or right edge). What about 2mm squares? You now can't use the row 1mm below the top, or 1mm to the left of the right-hand edge, so there are $179\times 279$ remaining places. Similarly the number of $k~\text{mm}$ by $k~\text{mm}$ squares is $(181-k)(281-k)$, or for a general $x$ by $y$ rectangle with $x\leq y$, $(x+1-k)(y+1-k)$.
So your answer is $\sum_{k=1}^{x}(x+1-k)(y+1-k)=\sum_{k=1}^{x}((x+1)(y+1)-(x+y+2)k+k^2)$. You can substitute in the formulae for the sum of the first $x$ positive integers and the sum of their squares to get $$x(x+1)(y+1)-\frac{x(x+1)}2(x+y+2)+\frac{x(x+1)(2x+1)}6,$$ which simplifies to $$\frac{x(x+1)(y-x)}2+\frac{x(x+1)(2x+1)}6.$$