General Aptitude

2D and 3D patterns

Part 1 of 3 · 16 min

Counting triangles and squares

The concept

Start with something familiar

How many squares are on a chessboard? Most people say 64 and stop. But the whole board is a square too, and so is every 2 by 2 block, every 3 by 3 block, and so on. The true answer is 204.
Counting questions in GATE work exactly like this. The figure is simple, but the small shapes hide inside bigger ones, and you lose marks by missing some or counting some twice. The cure is never to count by eye. Count by size, or by choosing lines, so that every shape is counted exactly once.

Idea 1: in an n by n grid, a k by k square fits in (n − k + 1)² places

Take a 3 by 3 grid of small squares. Where can a 2 by 2 square sit? Its top-left corner can be in the first or second column (2 choices) and in the first or second row (2 choices). That's 2×2=42 \times 2 = 4 places. In general, a kk by kk square can slide into n−k+1n - k + 1 positions across and the same number down.
SizeAcrossDownCount
1 by 1339
2 by 2224
3 by 3111
Total14
Play with it

Each dot is a place for the 2 by 2 square's top-left corner: 2 × 2 = 4. All sizes: 9 + 4 + 1 = 14.

Adding over every size gives the sum of the first nn squares:squares in an n×n grid=12+22+⋯+n2=n(n+1)(2n+1)6\begin{aligned} &\text{squares in an } n \times n \text{ grid} \\ &= 1^2 + 2^2 + \dots + n^2 \\ &= \frac{n(n+1)(2n+1)}{6} \end{aligned}
Example
How many squares are on an 8 by 8 chessboard?
8×9×176=204\dfrac{8 \times 9 \times 17}{6} = 204.

Idea 2: in a rectangular grid, choose two lines each way

Now take a grid that is mm squares tall and nn squares wide. Squares still slide, but the counts across and down differ:squares of size k=(m−k+1)(n−k+1)\begin{aligned} &\text{squares of size } k \\ &= (m - k + 1)(n - k + 1) \end{aligned}Here kk can be at most the shorter side.
For rectangles (which include squares), there's a neater trick. Every rectangle is fixed by choosing 2 of the horizontal grid lines (its top and bottom) and 2 of the vertical grid lines (its two sides). A grid mm tall has m+1m + 1 horizontal lines, and one nn wide has n+1n + 1 vertical lines:rectangles=(m+12)×(n+12)\text{rectangles} = \binom{m+1}{2} \times \binom{n+1}{2}
Example
Count the squares and the rectangles in a grid 2 squares tall and 3 wide.
Squares: 2×3=62 \times 3 = 6 of size 1 and 1×2=21 \times 2 = 2 of size 2, so 8. Rectangles: there are 3 horizontal and 4 vertical lines, so (32)×(42)=3×6=18\dbinom{3}{2} \times \dbinom{4}{2} = 3 \times 6 = 18.

Idea 3: lines from one corner make triangles in pairs

Draw a triangle, then draw some straight lines from its top corner down to the base. Every small triangle you can see has the top corner as one vertex, and its two slanted sides are two of the lines from the top (the two original sides count as lines too). The base supplies the third side.
So if there are nn lines from the top corner in total, each pair of them makes one triangle:triangles=(n2)\text{triangles} = \binom{n}{2}If the figure also has lines running across from one side to the other, parallel to the base, each such line (and the base itself) is a new base that works the same way. With hh bases:triangles=h×(n2)\text{triangles} = h \times \binom{n}{2}
Lines nnBases hhTriangles
311×3=31 \times 3 = 3
411×6=61 \times 6 = 6
422×6=122 \times 6 = 12
633×15=453 \times 15 = 45
Example
A triangle has two extra lines from its top corner to the base. How many triangles are there?
Lines from the top: 2 sides plus 2 extra, so n=4n = 4. Triangles =(42)=6= \dbinom{4}{2} = 6.

Idea 4: when there's no formula, label the pieces and count by how many pieces join

For an irregular figure, give every smallest region a number. Then list triangles made of 1 piece, then 2 pieces, then 3, and so on. Writing the list stops you from counting the same triangle twice.
Take a square with both diagonals drawn. The diagonals cut it into 4 small triangles; label them 1 (bottom), 2 (right), 3 (top), 4 (left).
Pieces joinedTrianglesCount
11, 2, 3, 44
21+2, 2+3, 3+4, 4+14
3none0
Total8
Each two-piece triangle is half of the square, cut along a diagonal. Two pieces across from each other (1 and 3) only touch at the centre, so they never make a triangle.
Example
Why is 1+2+3 not a triangle?
Three of the four pieces form the square with one quarter missing, a shape with five sides. Joining pieces only gives a triangle if the outline has exactly three straight sides.

Idea 5: in a triangular grid, count the upright and upside-down triangles separately

Divide each side of a big triangle into nn equal parts and draw lines parallel to the sides. You get a grid of small triangles, some pointing up and some pointing down. Upright triangles come in every size from 1 to nn. Upside-down ones only fit up to about half the size, because they need room above and below.
For n=3n = 3:
SizeUprightUpside-down
163
230
310
Total103
So a triangle split into 3 parts on each side has 10+3=1310 + 3 = 13 triangles. The totals for n=1,2,3,4n = 1, 2, 3, 4 are 1, 5, 13 and 27.
Example
How many triangles are in a triangular grid with 4 parts on each side?
Upright: 10+6+3+1=2010 + 6 + 3 + 1 = 20. Upside-down: 6 of size 1 and 1 of size 2, so 7. Total 20+7=2720 + 7 = 27.

How to spot which idea you need

The question says...Use
squares in a square gridIdea 1: 12+22+⋯+n21^2 + 2^2 + \dots + n^2
a rectangular grid, or "rectangles"Idea 2: choose 2 lines each way
lines fanning out from one cornerIdea 3: h×(n2)h \times \binom{n}{2}
an irregular figureIdea 4: label pieces, count by size
a triangle cut into small trianglesIdea 5: upright plus upside-down

Worked example 1warm-up

How many squares and how many rectangles are there in a 5 by 5 grid?
Solution
Step 1. Squares by sizeIdea 1
25+16+9+4+1=5525 + 16 + 9 + 4 + 1 = 55.
Step 2. Rectangles by choosing linesIdea 2
A 5 by 5 grid has 6 lines each way: (62)×(62)=15×15=225\dbinom{6}{2} \times \dbinom{6}{2} = 15 \times 15 = 225.
Step 3. Check
The formula gives 5×6×116=55\dfrac{5 \times 6 \times 11}{6} = 55 squares, matching Step 1. And 225 is more than 55, as it must be, since every square is also a rectangle.

Worked example 2GATE level

A square has both diagonals drawn, and also the two lines joining the midpoints of opposite sides (one across, one down). How many triangles does the figure contain?
Solution
Step 1. Find the smallest piecesIdea 4
The four lines all pass through the centre and cut the square into 8 thin triangles. Number them 1 to 8 going round the centre, starting with the left half of the bottom edge.
Step 2. Two pieces
Pieces 1 and 2 meet along the line from the centre down to the middle of the bottom edge, and together they form the triangle with corners at the centre and the two bottom corners of the square. The same happens on each side: 1+2, 3+4, 5+6, 7+8. That's 4. Pairs such as 2+3 form a small square, not a triangle.
Step 3. Three pieces
Any three neighbouring pieces have a bent outline with four or more sides, so 0.
Step 4. Four pieces
Half of the big square, cut along a diagonal: 1+2+3+4, 3+4+5+6, 5+6+7+8 and 7+8+1+2. That's 4.
Step 5. Total
8+4+0+4=168 + 4 + 0 + 4 = 16 triangles.
Step 6. Check
Each diagonal splits the square into 2 halves, giving 2×2=42 \times 2 = 4 big triangles, which matches Step 4. And Idea 4's square with only the diagonals had 8 triangles (4 quarters and 4 halves). All 8 are still here, and the middle lines add the 8 thin pieces, giving 16.

All the equations at a glance

IdeaEquation
Squares, n×nn \times n gridn(n+1)(2n+1)6\dfrac{n(n+1)(2n+1)}{6}
Squares, m×nm \times n grid∑(m−k+1)(n−k+1)\sum (m-k+1)(n-k+1)
Rectangles(m+12)×(n+12)\binom{m+1}{2} \times \binom{n+1}{2}
Fan from a cornerh×(n2)h \times \binom{n}{2}
Irregular figurelabel pieces, count by size
Triangular gridupright plus upside-down