When m=n
its a trivial case a square
so no. of squares it passes through is m
When m not equal to n
Now consider ur rectangle at the origin
The equation of diagonal is y=(m/n)*x
Now position the rectangle such that m>n
All the lines are of the form y= constant or x= constant
and the different lines have areas bounded between them
All you need now is
solutions of the equation y=(m/n)*x
where either of the coordinate is an integer
This can be generalised to y=m+n-1 when m and n are coprime
[1 for (m,n)]
this follows because if coprime
u cant find another ratio p/q = m/n
such that p and q are integers
when m not equal to n and they have a common factor
write 1,2,3,4,5,...m and 1,2,3,4,5...n
find all those points that satisfy y=(m/n)*x where both are integers
(called lattice points)
let the no. of such points be p
now answer in case 2 is m+n-1-p
for a good example
consider a rectangle sides 6/ 3
y= 6/3*x is the equation of the diagonal
now lattice points are (1,2) (2,4) =2
so no of squares the diagonal passes thru is
6+3-1-2 =6
when both are coprime
consider y=(5/3)*x
accordin 2 the logic no of squares= 5+3-1 =7