- Advanced
- About 12 min
- 5 practice questions
Counting rectangles and squares in a grid
Count every rectangle and every square in an m by n grid with exact formulas instead of by eye.
If something looks wrong, please tell us.
Counting figures is a favourite test of care. Counting by eye misses large rectangles that are made of several small cells. Formulas make the count exact in seconds, and they let you attempt the largest grids without fear.
Rectangles
A grid that is m cells wide and n cells high has m + 1 vertical lines and n + 1 horizontal lines. A rectangle (a square counts as a rectangle) is fixed by choosing 2 vertical lines and 2 horizontal lines. So the number of rectangles is C(m + 1, 2) x C(n + 1, 2), where C(k, 2) = k(k - 1)/2.
Squares
Squares of side k (in cell units) can be placed in (m - k + 1) positions across and (n - k + 1) positions down, so there are (m - k + 1)(n - k + 1) of them. Add this for k = 1, 2, ... up to the smaller of m and n. For an n by n grid the total is n(n + 1)(2n + 1)/6.
Worked examples
Example 1. A grid 4 cells wide and 3 high. Rectangles: C(5, 2) x C(4, 2) = 10 x 6 = 60. Squares: side 1 gives 4 x 3 = 12; side 2 gives 3 x 2 = 6; side 3 gives 2 x 1 = 2. Total squares: 12 + 6 + 2 = 20. So 60 - 20 = 40 of the rectangles are not squares.
Example 2. A 5 by 5 grid. Rectangles: C(6, 2) x C(6, 2) = 15 x 15 = 225. Squares: 25 + 16 + 9 + 4 + 1 = 55, which agrees with the formula 5 x 6 x 11 / 6 = 55.
| Grid | Rectangles | Squares |
|---|---|---|
| 1 by 1 | 1 | 1 |
| 2 by 2 | 9 | 5 |
| 3 by 3 | 36 | 14 |
| 4 by 4 | 100 | 30 |
| 5 by 5 | 225 | 55 |
Rectangles = C(m + 1, 2) x C(n + 1, 2). Squares = the sum of (m - k + 1)(n - k + 1) for k = 1, 2, 3, ...
Key takeaways: choose two vertical and two horizontal lines for rectangles, and add by side length for squares.
Check your understanding
-
1. How many rectangles (squares included) are there in a grid 5 cells wide and 2 cells high?
- 30
- 45
- 36
- 60
Show answer
Answer: B. There are 6 vertical and 3 horizontal lines. Rectangles = C(6, 2) x C(3, 2) = 15 x 3 = 45.
-
2. How many squares are there in a 4 by 4 grid?
- 16
- 25
- 30
- 100
Show answer
Answer: C. Side 1: 16, side 2: 9, side 3: 4, side 4: 1. Total 30, matching n(n + 1)(2n + 1)/6 = 4 x 5 x 9/6 = 30. (100 is the number of rectangles.)
-
3. How many squares are there in a grid 5 cells wide and 3 cells high?
- 26
- 22
- 15
- 30
Show answer
Answer: A. Side 1: 5 x 3 = 15. Side 2: 4 x 2 = 8. Side 3: 3 x 1 = 3. Total 15 + 8 + 3 = 26. Side 4 does not fit because the grid is only 3 high.
-
4. In a grid 3 cells wide and 2 cells high, how many of the rectangles are NOT squares?
- 8
- 18
- 12
- 10
Show answer
Answer: D. Rectangles: C(4, 2) x C(3, 2) = 6 x 3 = 18. Squares: side 1 gives 3 x 2 = 6, side 2 gives 2 x 1 = 2, so 8. Not squares: 18 - 8 = 10.
-
5. How many squares are there in a 6 by 6 grid?
- 36
- 91
- 441
- 55
Show answer
Answer: B. 36 + 25 + 16 + 9 + 4 + 1 = 91, or n(n + 1)(2n + 1)/6 = 6 x 7 x 13/6 = 91. 441 = 21 x 21 is the number of rectangles, and 55 is for a 5 by 5 grid.
Sign in to keep track of the lessons you finish.