$H(n)=\lfloor\dfrac{b}{n}\rfloor- \lfloor \dfrac{a}{n} \rfloor=$ (roughly) # odd pairs $o, o+2 \in [a,b]$ such that $n \mid o$ or $n \mid o+2$

You have that $\lfloor\frac{b}{n}\rfloor$ counts how many numbers less or equal to b are multiple of $n$. From these you have to subtract those multiple of $2n$ to have only the odd ones, getting $\lfloor\frac{b}{n}\rfloor-\lfloor\frac{b}{2n}\rfloor$. Now if you want only those in $[a,b]$ you have to subtract the count for $a−1$ to get: $\lfloor\frac{b}{n}\rfloor-\lfloor\frac{b}{2n}\rfloor-\lfloor\frac{a-1}{n}\rfloor+\lfloor\frac{a-1}{2n}\rfloor$. After this, everything must be multiplied by $2$ because most of those numbers belong to $2$ couples, but we need to subtract the contribution of $a$ and $b$ if these are multiples of $n$: this can be accomplished with $\lfloor\frac{b}{n}\rfloor-\lfloor\frac{b-1}{n}\rfloor$ and $\lfloor\frac{a}{n}\rfloor-\lfloor\frac{a-1}{n}\rfloor$. So we finally get:

$$\begin{aligned} H(a,b,n) &= 2\left(\lfloor\frac{b}{n}\rfloor-\lfloor\frac{b}{2n}\rfloor-\lfloor\frac{a-1}{n}\rfloor+\lfloor\frac{a-1}{2n}\rfloor\right)-\lfloor\frac{b}{n}\rfloor+\lfloor\frac{b-1}{n}\rfloor-\lfloor\frac{a}{n}\rfloor+\lfloor\frac{a-1}{n}\rfloor \\&= \lfloor\frac{b}{n}\rfloor-2\lfloor\frac{b}{2n}\rfloor-\lfloor\frac{a-1}{n}\rfloor+2\lfloor\frac{a-1}{2n}\rfloor+\lfloor\frac{b-1}{n}\rfloor-\lfloor\frac{a}{n}\rfloor \end{aligned}$$


Let $n\ge 3$.

Let $M$ be the number of odd numbers $m$ such that $$a\le mn\le b-2$$ Also, $K$ be the number of odd numbers $k$ such that $$a+2\le kn\le b$$

Then, one has $H(a,b,n)=M+K$.

Now, note that $$a\le mn\le b-2\iff\left\lceil\frac{a}{n}\right\rceil\le m\le \left\lfloor\frac{b-2}{n}\right\rfloor$$ and $$a+2\le kn\le b\iff\left\lceil\frac{a+2}{n}\right\rceil\le k\le\left\lfloor\frac{b}{n}\right\rfloor$$

  • If $\left\lceil\frac{a}{n}\right\rceil$ is odd and $\left\lfloor\frac{b-2}{n}\right\rfloor$ is odd, then $M=\frac{\left\lfloor\frac{b-2}{n}\right\rfloor-\left\lceil\frac{a}{n}\right\rceil+2}{2}$

  • If $\left\lceil\frac{a}{n}\right\rceil$ is odd and $\left\lfloor\frac{b-2}{n}\right\rfloor$ is even, then $M=\frac{\left\lfloor\frac{b-2}{n}\right\rfloor-\left\lceil\frac{a}{n}\right\rceil+1}{2}$

  • If $\left\lceil\frac{a}{n}\right\rceil$ is even and $\left\lfloor\frac{b-2}{n}\right\rfloor$ is odd, then $M=\frac{\left\lfloor\frac{b-2}{n}\right\rfloor-\left\lceil\frac{a}{n}\right\rceil+1}{2}$

  • If $\left\lceil\frac{a}{n}\right\rceil$ is even and $\left\lfloor\frac{b-2}{n}\right\rfloor$ is even, then $M=\frac{\left\lfloor\frac{b-2}{n}\right\rfloor-\left\lceil\frac{a}{n}\right\rceil}{2}$

Therefore, one can write $$M=\frac{\left\lfloor\frac{b-2}{n}\right\rfloor-\left\lceil\frac{a}{n}\right\rceil+\frac{1-(-1)^{\left\lfloor\frac{b-2}{n}\right\rfloor}}{2}+\frac{1-(-1)^{\left\lceil\frac{a}{n}\right\rceil}}{2}}{2}$$ i.e. $$M=\frac{2\left\lfloor\frac{b-2}{n}\right\rfloor-2\left\lceil\frac{a}{n}\right\rceil-(-1)^{\left\lfloor\frac{b-2}{n}\right\rfloor}-(-1)^{\left\lceil\frac{a}{n}\right\rceil}+2}{4}$$

Therefore, one has $$H(a,b,n)=M+K=\frac{2\left\lfloor\frac{b-2}{n}\right\rfloor-2\left\lceil\frac{a}{n}\right\rceil-(-1)^{\left\lfloor\frac{b-2}{n}\right\rfloor}-(-1)^{\left\lceil\frac{a}{n}\right\rceil}+2}{4}+\frac{2\left\lfloor\frac{b}{n}\right\rfloor-2\left\lceil\frac{a+2}{n}\right\rceil-(-1)^{\left\lfloor\frac{b}{n}\right\rfloor}-(-1)^{\left\lceil\frac{a+2}{n}\right\rceil}+2}{4}$$