TITLE: Number of unit squares that meet a given diagonal line segment in more than one point
QUESTION [7 upvotes]: Let $l$, $b$ be positive integers. Divide the $l \times b$ rectangle into $lb$ unit squares in the usual manner. Consider one of the two diagonals of this rectangle. How many of these unit squares contains a segment of positive length of this diagonal?

REPLY [6 votes]: The diagonal line will cross $b$ squares (in the $b$ direction) and $l$ squares (in the ($l$ direction), These ($b$ and $l$ directions) will count separately toward the total number of squares cut except for $\gcd(l,b)$ occasions when the diagonal makes the transition from one row to the next in both directions through a grid point. This counts the start/end corners as "half a grid point" each, by analogy with the $2b \times 2l$ rectangle. 
Thus the total number of squares with a portion of the diagonal will be $l+b-\gcd(l,b)$.

For the illustration, $l=8, b=12$ and the diagonal goes through $\gcd(12,8)=4$ grid points for a total of $12+8-4=16$ squares cut.