§ Cost of looping over all multiples of i for i in 1 to N
- Intuitively, when I think of "looping over i and all its multiples", I seem to have a gut feeling that its cost is N. Of course, it is not. It is N/i.
- Thus, the correct total cost becomes ∑i=1NN/i (versus the false cost of ∑i=1NN=N2.
- The correct total cost is a harmonic series N⋅∑i=1N1/i≃NlogN.
- This is useful for number theory problems like 1627D