English

Computing $\pi(N)$: An elementary approach in $\tilde{O}(\sqrt{N})$ time

Number Theory 2023-08-15 v4

Abstract

We present an efficient and elementary algorithm for computing the number of primes up to NN in O~(N)\tilde{O}(\sqrt N) time, improving upon the existing combinatorial methods that require O~(N2/3)\tilde{O}(N ^ {2/3}) time. Our method has a similar time complexity to the analytical approach to prime counting, while avoiding complex analysis and the use of arbitrary precision complex numbers. While the most time-efficient version of our algorithm requires O~(N)\tilde{O}(\sqrt N) space, we present a continuous space-time trade-off, showing, e.g., how to reduce the space complexity to O~(N3)\tilde{O}(\sqrt[3]{N}) while slightly increasing the time complexity to O~(N8/15)\tilde{O}(N^{8/15}). We apply our techniques to improve the state-of-the-art complexity of elementary algorithms for computing other number-theoretic functions, such as the the Mertens function (in O~(N)\tilde{O}(\sqrt N) time compared to the known O~(N0.6)\tilde{O}(N^{0.6})), summing Euler's totient function, counting square-free numbers and summing primes. Implementation code is provided.

Keywords

Cite

@article{arxiv.2212.09857,
  title  = {Computing $\pi(N)$: An elementary approach in $\tilde{O}(\sqrt{N})$ time},
  author = {Dean Hirsch and Ido Kessler and Uri Mendlovic},
  journal= {arXiv preprint arXiv:2212.09857},
  year   = {2023}
}
R2 v1 2026-06-28T07:43:21.831Z