Verifying $\sum_{n\leq x, (n, k) = 1} \frac{1}{n} = \frac{\phi(k)}{k}\log x + O(1)$. I am not able to find this problem on Math.SE before, and I also want to make sure my solution is correct. Can someone help look over it?
$$\begin{align*}
\sum_{n\leq x, (n, k) = 1} \frac{1}{n} &= \sum_{n\leq x} \frac{1}{n}\sum_{d\mid n, d\mid k}\mu(d) \\ &= \sum_{d\mid k}\mu(d)\sum_{d\mid n,n\leq x}\frac{1}{n}\\&=\sum_{d\mid k}\frac{\mu(d)}{d}\sum_{n\leq\frac{x}{d}}\frac{1}{n}\\&=\sum_{d\mid k}\frac{\mu(d)}{d}\left(\log\left(\frac{x}{d}\right) + O(1)\right) \\ &= \sum_{d\mid k}\frac{\mu(d)}{d}(\log x + O(\log d) + O(1)) \\ &= \log x\cdot\sum_{d\mid k}\frac{\mu(d)}{d} + {\color{red}{O\left(\sum_{d\mid k}\frac{\mu(d)\log d}{d}\right)}} + O\left(\sum_{d\mid k}\frac{\mu(d)}{d}\right) \\ &= \log  x\cdot\prod_{p\mid k}\left(1 - \frac{1}{p}\right) + O(1) \\ &= \frac{\phi(k)}{k}\log x + O(1)
\end{align*}$$
In particular, I simplified the red term as follows:
$$\begin{align*}
\sum_{d\mid k}\frac{\mu(d)\log d}{d} \ll \sum_{d\mid k}\frac{\mu(d)}{d^{\frac{1}{2}}} = \prod_{p\mid k}\left(1 - \frac{1}{\sqrt{p}}\right) \ll 1
\end{align*}$$
Since each term in the product is negative. Is this correct? And how can the red term be simplified simpler? The method I used seems kind of indirect and I imagine there are better ways to prove the result.
Thank you!
 A: In order to get a smaller $O$-bound, one can use the formula
$$
\sum_{n\leq x} \frac{1}{n} = \log(x) + C + O\left(\frac{1}{x}\right),
$$
where $C = \lim\limits_{n\to \infty}\left(1+ \frac12 + \dots \frac1n - \log(n) \right) $ is the Euler constant. This approach gives
\begin{align*}
\sum_{\substack{n\leq x \\ (n,k)=1}} \frac{1}{n} & = \sum_{d|k } \frac{\mu(d)}{d} \sum_{n\leq \frac{x}{d}} \frac{1}{n} \\ 
& =\sum_{d|k} \frac{\mu(d)}{d} \left(\log \frac{x}{d} + C + O\left(\frac{d}{x}\right)\right)\\
 &= \sum_{d|k} \frac{\mu(d)}{d} \left(\log x +C\right) \underbrace{- \sum_{d|k} \mu(d) \frac{\log(d)}{d}}_{:= C_k} + O\left(\frac{2^{\omega(k)}}{x}\right) \\ 
&= \frac{\varphi(k)}{k} \left(\log x + C\right) + C_k + O\left(\frac{2^{\omega(k)}}{x}\right).
\end{align*}
Here $C_k$ is just a specific constant, depending on $k.$
This result is more precise since it has a $O_k(1/x)$ bound rather than a $O(1)$ bound.
In your simplification of the red term, be aware that
$$
\sum_{n\leq x} f(n) \ll \sum_{n \leq x} |f(n)|,
$$
so you need extra absolute values.
I hope this helps.
A: None of the big-$O$ terms should have $\mu(d)$ in them: When writing a term as part of a big-$O$ expression, all sign information/cancellation is lost. Therefore the error produced by this method is
$\displaystyle\sum_{d\mid k} \frac{\log d}d$, which can be larger than any power of $\log k$.
A: This answer is dedicated to evaluate the $C_k$ explicitly so that we can get a grasp about its growth behavior:
\begin{aligned}
-C_k=\sum_{d|k}{\mu(d)\log d\over d}
&=\sum_{\substack{d|k\\d\text{ squarefree}}}{\mu(d)\over d}\sum_{p|d}\log p \\
&=\sum_{p|k}\log p\sum_{\substack{d|k\\p|d}}{\mu(d)\over d}=\sum_{p|k}\log p\sum_{\substack{t|k\\p\nmid t}}{\mu(pd)\over pd} \\
&=-\sum_{p|k}{\log p\over p}\sum_{\substack{t|k\\p\nmid t}}{\mu(t)\over t}=-\sum_{p|k}{\log p\over p}\prod_{p'|k,p'\ne p}\left(1-{1\over p'}\right) \\
&=-{\varphi(k)\over k}\sum_{p|k}{\log p\over p(1-p^{-1}}=-{\varphi(k)\over k}\sum_{p|k}{\log p\over p-1}.
\end{aligned}
Therefore, for any $1\le T\le k$ there is
\begin{aligned}
|C_k|
&\le\sum_{p|k}{\log p\over p-1}\le\sum_{p\le T}{\log p\over p-1}+{1\over T-1}\sum_{p|k}\log p \\
&\le\sum_{p\le T}{\log p\over p-1}+{\log k\over T-1}\ll\log T+{\log k\over T}
\end{aligned}
Setting $T=\log3k$ gives $C_k=O(\log\log3k)$, so we have
\begin{aligned}
\sum_{\substack{n\le x\\(n,k)=1}}\frac1n
&=\sum_{d|k}{\mu(d)\over d}(\log x+O(1))+C_k \\
&={\varphi(k)\over k}\log x+C_k+O\left(\prod_{p|k}\left(1+\frac1p\right)\right) \\
&={\varphi(k)\over k}\log x+O(\log\log3k)+\color{blue}{O\left(\exp\left(1+\sum_{p|k}\frac1p\right)\right)} \\
&={\varphi(k)\over k}\log x+O(\log\log3k)
\end{aligned}
For the blue part, it suffices to notice that
$$
\sum_{p|k}\frac1p\ll\sum_{p\le\log3k}\frac1p+\sum_{p|k}{\log p\over\log3k}\ll\log\log\log3k.
$$
