scratch

§ Cost of Looping over All Multiples of iii for iii in 111 To NNN

created 2022-01-18
  • Intuitively, when I think of "looping over iii and all its multiples", I seem to have a gut feeling that its cost is NNN. Of course, it is not. It is N/iN/iN/i.
  • Thus, the correct total cost becomes ∑i=1NN/i\sum_{i=1}^N N/i∑i=1N​N/i (versus the false cost of ∑i=1NN=N2\sum_{i=1}^N N = N^2∑i=1N​N=N2.
  • The correct total cost is a harmonic series N⋅∑i=1N1/i≃Nlog⁡NN\cdot \sum_{i=1}^N1/i \simeq N \log NN⋅∑i=1N​1/i≃NlogN.
  • This is useful for number theory problems like 1627D
❦
Newer ৪ Blog ৪ Older