I was trying to see if I could prove the Prime Number Theorem using Legendre's formula. I did it the following way:
We hae
$$\log n!=n(\log n-1)+O(\log n)=\sum_{p\leq n}\frac{n-s_p(n)}{p-1}\log p=(n-O(\log n))\sum_{p\leq n}\frac{\log p}{p-1}$$
Coming from Stirling's approximation and Legendre's formula.
Then,
\begin{align} \sum_{p\leq n}\frac{\log p}{p-1}&=\frac{n}{n-O(\log n)}(\log n-1)+O\left(\frac{\log n}n\right) \\ &= \left(1+O\left(\frac{\log n}n\right)\right)(\log n-1)+O\left(\frac{\log n}n\right) \\ &=\log n-1+O\left(\frac{\log^2 n}{n}\right)=f(n) \end{align}
Which we can then differentiate:
$$\frac{df}{dn}=\frac{1}{n}+O\left(\frac{\log^2 n}{n^2}\right)=\frac{d\pi}{dn}\frac{\log n}{n-1}$$
Because there is a $\frac{d\pi}{dn}$ chance that $n$ is prime and that $\frac{\log n}{n-1}$ will be added to the sum.
Then,
\begin{align} \frac{d\pi}{dn}&=\frac{n-1}{\log n}\left(\frac{1}{n}+O\left(\frac{\log^2 n}{n^2}\right)\right)\\ &=\frac{n-1}{n}\frac{1}{\log n}+O\left(\frac{\log n}{n}\right)\\ &=\frac{1}{\log n}+O\left(\frac{\log n}{n}\right)\\ \end{align}
And finally with integration, we get
$$\pi(n)=\operatorname{li}(n)+O\left(\log^2 n\right)$$
Which is a very very very strong error bound on $\pi(n)-\operatorname{li}(n)$, considering the best known asymptotic (even after assuming RH) is
$$\pi(n)=\operatorname{li}(n)+O(\sqrt n\log n)$$
So where have I gone wrong, and what would this method give me in the end? I suspect it's in the imprecise definition of differentiation but I'm not sure.
Your first equation seems to be using an estimate $s_p(n) = O(\log n)$, where $s_p(n)$ is the number of digits in the base-$p$ expansion of $n$. However, $n$ has $\lfloor \frac{\log n}{\log p} \rfloor + 1$ digits in base $p$, and each digit can be as large as $p-1$; therefore the best estimate one can get for $s_p(n)$ is around $\frac p{\log p} \log n$. And since $p$ can be close to $n$, the $\frac p{\log p}$ term is huge....