Is there a formula to evaluate the number of all squares in the $m \times n$ grid? Well, I'm just curious, I've seen the question like this somewhere at the university, to solve this they were dividing the grid with $m - 1$ and $n - 1$ lines...I don't know what's next.
-
2(m)x(n) + (m-1)x(n-1) + (m-2)x(n-2)... until you get the first zero term. – udiboy1209 Aug 15 '13 at 10:21
3 Answers
Suppose $n\ge m$.
- Number of squares of size 1: $m\cdot n$
- Number of squares of size 2: $(m-1)\cdot (n-1)$
- ...
- Number of squares of size m: $1\cdot (n-m+1)$
Result: $$\begin{align} \sum_{k=1}^m k \cdot (n-m+k) & =(n-m)\sum_{k=1}^m k +\sum_{k=1}^m k^2 \\ & = (n-m) m(m+1)/2 + m(m+1)(2m+1)/6 \\ & = \frac{m(m+1) (3n-m+1)}{6}\end{align}$$
Here's an induction approach. Let's pretend we don't know the formula as yet. Let $f(n, m)$ be the number of squares in a $n \times m$ rectangle. WLOG $n \geq m$
When $n =m$, we know that there are $ f(m, m) = \sum_{k=1}^m k^2 = \frac{ m(m+1)(2m+1)}{6} $ squares.
Suppose we have the value $f(n, m)$. Consider adding another row at the bottom. How many squares do we introduce, that involve that bottom row? If we pick any 2 vertical grid lines, they uniquely define a square that involves the bottom row, hence we're adding ${ m +1 \choose 2 } = \frac{ m(m+1) } { 2}$ squares. Thus, there are $f(n+1, m) = f(n, m) + \frac{ m(m+1) } { 2}$ squares.
This tells us that, for a fixed $m$,
- $f(n+1, m)$ is linear, with a slope of $\frac{m(m+1) } { 2}$.
- $f(m, m ) = \frac{ m(m+1)(2m+1)}{6}$.
- Using point slope form, we conclude that $f(n, m) = \frac{ m(m+1) ( 3n-m + 1 ) } { 6}$.
- 77,541
Rectangles in rectangle $$\frac{(n^2+n)(m^2+m)}{4}$$
Rectangles in square $$\frac{(n^2+n)^2}{4}$$
Squares in rectangle $$m≥ n-1,\frac{(n^2+n)}{2}m-\frac{(n^3-n)}{6}$$
Squares in square $$\frac{(n^2+n)(2n+1)}{6}$$
- 205
-
-
@JoeTait: There are $n+1$ horizontal lines, of which you pick two. There are $m+1$ vertical lines, of which you pick two. – Ross Millikan Feb 25 '14 at 19:16
-
@RossMillikan How can I get the total number of rectangles in different sizes in a rectangular grid of mXn. For example : let say m=2 and n=2, have total of 9 rectangles. 4 are of size 1, 4 are of size 2 and 1 is of size 3. than how to calculate all the rectangles of size 1X1, 1X2, 2X1, 2X2 .... – pa1pal Feb 09 '16 at 16:40
-
1@pa1pal: Let us have a grid $6 \times 8$ and look for the number of $2 \times 3$ rectangles. There are $7$ horizontal lines and you can choose any of $5$ of them to be the top edge of your rectangle. There are $9$ vertical lines and you can choose any of $6$ of them to be the left edge. There are therefore $5 \cdot 6=30$ rectangles that are $2$ high and $3$ wide. – Ross Millikan Feb 09 '16 at 16:48
-
@RossMillikan Got it. If I want to look for 1 x 2 so I have to choose any 6 rows and any 7 vertical lines. Total of 6*7=42 rectangles. Now In a 6 x 8 grid there are total of 756 unit squares present. If I want to have a number of rectangles which comprises of 1 square, 2 square, 3 square and so on ... One way is I can calculate all the 1 x 2 and 2 x 1 rectangles and add them for the case of 2 squares. Is there any other way ? – pa1pal Feb 09 '16 at 17:02
-
@pa1pal: how do you get 756 unit squares in a 6 x 8 grid? I only get 48. – Ross Millikan Feb 09 '16 at 19:52
-
@RossMillikan sorry not unit squares. But the total number of rectangles in a m x n grid. Which is (n)(n+1)*(m)(m+1) / 4 – pa1pal Feb 10 '16 at 11:04