[Math] A question about $ (2 \times 3) $-rectangles.

combinatorial-geometrycombinatoricsgeometry

The following is a problem from TopCoder:

Problem. Given the width and the height of a rectangular grid, return the total number of non-square rectangles that can be found on the grid.

For example, for a grid with width $ 3 $ and height $ 3 $, i.e.,

3-by-3 grid

there are

  • four $ (2 \times 3) $-rectangles,
  • six $ (1 \times 3) $-rectangles and
  • twelve $ (1 \times 2) $-rectangles.

There are thus a total number of $ 4 + 6 + 12 = 22 $ rectangles. Note that we don’t count $ (1 \times 1) $-, $ (2 \times 2) $- or $ (3 \times 3) $-rectangles because these are actually squares.

I have assumed that “$ 2 \times 3 $” means “$ 2 $ rows and $ 3 $ columns”. Is my assumption correct?

Can someone help me to find how many rectangles there are in the rectangular grid below?

5-by-7 grid

EDIT: The actual grid in the question is unknown, so a $ (5 \times 7) $-grid is being given as a working example.

Best Answer

Notation

I have assumed that “$2\times3$” means “$2$ rows and $3$ columns”. Is my assumption correct?

It depends on the context. In general I'd agree, but since the counts you quote doesn't include a separate handling of the $3\times2$ case, in this situation I'd rather assume that $2\times3$ is meant to cover both orientations, both two rows and three columns and two columns and three rows.

Total count

Can someone help me to find how many rectangles there are in the rectangular grid below?

If all you are interested in is the total number of non-square rectangles, then it doesn't matter how you write down each individual count. In fact, I'd not iterate over all possible shapes, but instead use a different approach.

For a grid of $m\times n$ tiles, there are $(m+1)\times(n+1)$ tile corners. If you consider each of them as a possible corner of a rectangle, and define each rectangle by two opposite corners, then you have a total of

$$\bigl((m+1)(n+1)\bigr)^2$$

possible combinations of two points. Obviously, that number is way too large, because we counted some things we shouldn't, and counted others more than once. So let's address those issues.

You don't want either dimension of such a rectangle to be zero. So if you picked the first point, then there are $m+n+1$ possible positions where that second point would be in the same row and/or the same column as the first. Subtract that and you are at

$$(m+1)(n+1)\bigl((m+1)(n+1)-(m+n+1)\bigr)=(m+1)(n+1)mn\;.$$

As Arthur pointed out in a comment, you can think of this as picking the second point from the $m\times n$ possible corner positions which you obtain by removing the “forbidden” row and column. At this point you have two points in distinct rows and columns, but nothing is sorted yet. So every possible rectangle is counted four times: any of its four corners might be the first point, with the opposite corner as the second point. So the number of rectangles is

$$\tfrac14(m+1)(n+1)mn\;.$$

Now you still have to get rid of those squares. How many are there? Let's look for a pattern. There are $m\times n$ squares of size $1$. There are $(m-1)\times(n-1)$ squares of size $2$, and so on. The maximal size such a square can have is the smaller of the dimensions of your tile grid. So there are

$$\sum_{i=1}^{\min(m,n)}(m+1-i)(n+1-i)$$

squares in total. Assuming $m\le n$ this simplifies to

$$\sum_{i=1}^{m}(m+1-i)(n+1-i)=\tfrac16 m(m+1)(3n-m+1)\;.$$

Now take the number of all rectangles, subtract all squares, and you are done. If you expand and then factor the result, you get a total count of

$$\tfrac1{12}m(m+1)(3n^2 + 2m - 3n - 2)= \tfrac1{12}m(m+1)\bigl(3n(n-1) + 2(m-1)\bigr)\;.$$

For $m=n=3$ this indeed yields a count of $22$. For $m=5,n=7$ (remember to ensure $m\le n$) this gives a count of $335$ non-square rectangles. You can verify that count with a brute-force computation.

Bonus question

If you want to, it might be interesting to think about why that formula always results in an integer if $m,n$ are integers.

Either $m$ or $m+1$ is divisible by two, so their product is even. For the same reason, $n(n-1)$ is even, and since $2(m-1)$ is even, too, $\bigl(3n(n-1) + 2(m-1)\bigr)$ must be even. So the whole thing is the product of two even numbers, and therefore divisible by four.

Exactly one of $m,(m+1),(m-1)$ is divisible by three. In the first two cases, the whole product is obviously divisible by three. In the last case, $\bigl(3n(n-1) + 2(m-1)\bigr)$ is divisible by three since it's the sum of two integers which are divisible by three. As the whole thing is divisible by four and by three, it is divisible by twelve.

Related Question