TITLE: How does summation formula work with floor function?
QUESTION [6 upvotes]: Prove that if a and b are relatively prime, then
  $$\sum_{n=1}^{a-1} \left\lfloor \frac{nb}{a}\right\rfloor = \frac{(a - 1)(b - 1)}{2}$$

My attempt was:
We have:
$$\sum_{i=1}^{n-1} i = \frac{n(n - 1)}{2}$$
Then, 
$$\sum_{n=1}^{a-1} \left\lfloor \frac{nb}{a}\right\rfloor = \left\lfloor \frac{a(a - 1)b}{2a}\right\rfloor$$
Could I apply the summation formula for floor function like above? Am I in the right track?  
Thanks,
Chan

REPLY [2 votes]: A different Hint:
Consider a square of side $ab$, with one corner at $(0,0)$ and the opposite corner at $(ab, ab)$. Draw the vertical lines $x = b$, $x=2b$ etc and the horizontal lines $y = a$, $y = 2a$ etc.  Now try to count in two different ways, the number of intersection points of these lines, which lie on or below the diagonal of the square.