How many rectangles can be seen in the figure, and how many of them are squares? (There are only two different distances between adjacent parallel lines, their ratio is $1: 2$.

![](https://cdn.mathpix.com/cropped/2024_05_02_f7d7d7aa1e130da3e5a8g-1.jpg?height=696&width=702&top_left_y=270&top_left_x=703)
The sides of the rectangles in the diagram are among the lines in the diagram. There are 31 horizontal and 31 vertical lines in the diagram. The first horizontal line can be chosen in 31 ways, and the second in 30 ways from the remaining 30. However, this way we have listed each possible choice twice: once when we choose the lower one first, and once when we choose the upper one. Therefore, the horizontal sides of a rectangle can be chosen in $\frac{30 \cdot 31}{2}=465$ ways from the lines in the diagram. Each of these can be paired with 465 ways to choose the vertical sides, so the total number of rectangles is $465^{2}=216225$.

![](https://cdn.mathpix.com/cropped/2024_05_02_8b5c78c0c64a7b460db2g-1.jpg?height=683&width=683&top_left_y=450&top_left_x=710)

A selected rectangle will be a square if the distance between its vertical sides is equal to the distance between its horizontal sides. Let's choose the unit to be the smallest distance between adjacent parallel lines. Then the side of the smallest square in the diagram is 1 unit, and the side of the largest is 45 units. Let's call the strip between parallel lines that are one unit apart a narrow strip, and the strip between adjacent lines that are 2 units apart a wide strip. Take any two arbitrary parallel lines and calculate how many narrow and how many wide strips are between them. Let the number of the former be $k$, and the number of the latter be $l$, then the distance between the two lines is $d=k+2 l$. Here, the difference between $k$ and $l$ can be at most 1, since the narrow and wide strips alternate, so $k=l+\varepsilon$, where $\varepsilon$ can be $-1, 0$, or +1, and the distance between the two lines is $d=3 l+\varepsilon$. According to this, the side length of the squares can be any integer between 1 and 45, and the side length uniquely determines the number of wide strips in the square: this is the nearest integer to one third of the side length that is divisible by 3. The side length and the number of wide strips also uniquely determine the number of narrow strips.

If the side length is divisible by 3, say $3 n$, then $l \leq n \leq 15, k=l=n$, and there are $2 n$ strips in the square. The first strip can be chosen from the 30 strips in the diagram in $(30-(2 n-1))=(31-2 n)$ ways, since $(2 n-1)$ strips must still fit after it. Therefore, the number of squares with side length $3 n$ is $(31-2 n)^{2}$, and the number of squares whose side length is divisible by 3 is

$$
A=29^{2}+27^{2}+\ldots+3^{2}+1^{2}
$$

If the side length is of the form $(3 n+1)$, then $0 \leq n \leq 14$, and $k=n+1, l=n$. In this case, the first and last strips are narrow, and the first strip can be chosen from the 15 narrow strips in $(15-n)$ ways. Therefore, the number of such squares is

$$
B=15^{2}+14^{2}+\ldots+2^{2}+1^{2}
$$

If the side length is of the form $(3 n-1)$, then $1 \leq n \leq 15$, and $k=n-1, l=n$. In this case, the outer strips are wide, and the first strip can be chosen in $(16-n)$ ways. The number of such squares is also $B$, and the total number of squares is $N=A+2 B$.

Note. We did not consider the numerical calculation of $N$ to be a part of the complete solution, since the solvers of the exercises have not yet learned the formula for the sum of the first $n$ squares:

$$
S_{n}=\frac{n(n+1)(2 n+1)}{6}
$$

According to this,

$$
N=S_{30}-2 \cdot S_{15}=6975
$$

since $B=S_{15}$, and $A=S_{30}-4 S_{15}$.