4. On each of the lines $x=5$ and $x=6$, there are 58 points with ordinates $1, 2, 3, \ldots, 58$. In how many ways can three points be chosen from the marked 116 so that they form the vertices of a right triangle?
Answer: 6724.

Solution. There are two possibilities.

1) The hypotenuse of the triangle lies on one of the lines, and the vertex of the right angle is on the second line. Let $ABC$ be the given triangle with a right angle at vertex $C$, and $CH$ be its height dropped to the hypotenuse. From the proportionality of the segments of the right triangle, we get that $CH^2 = AH \cdot BH$, i.e., $AH \cdot BH = 1$. Since $AH$ and $BH$ are integers, then $AH = BH = 1$.

The hypotenuse $AB$, equal to 2, can be placed in $56 \cdot 2 = 112$ ways (with $58 - 2$ ways of placement on each of the two given lines), and the position of vertex $C$ is uniquely determined.

2) One of the legs of the triangle (let's call it $BC$) is perpendicular to the given lines, and the other leg ($AC$) lies on one of the given lines. Then the position of the leg $BC$ can be chosen in 58 ways. For each variant of the placement of the leg $BC$, the vertex $A$ can be placed in 114 ways (all points except the already chosen $B$ and $C$) - in total, this gives $58 \cdot 114 = 6612$ ways.

In total, we get $112 + 6612 = 6724$ ways.