I'm looking to compute the integer difference between the $k$th and $(k+1)$th multiples of a fraction, i.e.
$$\left\lfloor{(k + 1)m \over n}\right\rfloor - \left\lfloor {km\over n}\right\rfloor$$
where all variables are positive integers (and they're not predetermined constants).
I'm already speeding this up by doing $km + m$ instead of $(k + 1)m$ so that I can factor out $km$, but unfortunately, this computation requires two integer divisions, and integer division is quite slow.
Is there a faster way to compute the same exact value?
I feel like the simplicity of the problem might admit a simple solution, but for some reason I'm not seeing one.
Provided the $\bmod$ operation is faster on whatever platform you'll be running your algorithm (it usually is), then $$\require{cancel} \frac{(\cancel{km}+m)-((km+m)\bmod n)-\cancel{km}+(km\bmod n)}{n} $$ matches your expression with just one integer division. In fact, the numerator must be a multiple of $n$. This is just an application of this formula $$ \left\lfloor \frac{x}{y} \right\rfloor y = x - (x \bmod y) $$ which is valid for positive $y$.