{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/computing-p-n-an-elementary-approach-in-tilde","title":"Computing $π(N)$: An elementary approach in $\\tilde{O}(\\sqrt{N})$ time","arxiv_id":"2212.09857","date":"2022-12-19","proceeding":null,"authors":["Dean Hirsch","Ido Kessler","Uri Mendlovic"],"abstract":"We present an efficient and elementary algorithm for computing the number of primes up to $N$ in $\\tilde{O}(\\sqrt N)$ time, improving upon the existing combinatorial methods that require $\\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 $\\tilde{O}(\\sqrt N)$ space, we present a continuous space-time trade-off, showing, e.g., how to reduce the space complexity to $\\tilde{O}(\\sqrt[3]{N})$ while slightly increasing the time complexity to $\\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 $\\tilde{O}(\\sqrt N)$ time compared to the known $\\tilde{O}(N^{0.6})$), summing Euler's totient function, counting square-free numbers and summing primes. Implementation code is provided.","url_abs":"https://arxiv.org/abs/2212.09857v4","url_pdf":"https://arxiv.org/pdf/2212.09857v4.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"links_only","authors_date_abstract":"arXiv metadata, CC0 1.0 (https://info.arxiv.org/help/license), from the Kaggle arXiv metadata snapshot of 2026-09-12"},"code_links":[{"paper_slug":"computing-p-n-an-elementary-approach-in-tilde","repo_url":"https://github.com/primecounting/primecounting","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"none","reach":null}],"tasks":[],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":null,"atlas_url":null,"mcp":null,"developers":"https://syntology.ai/developers"},"arxiv_metadata":null,"syntology_extracted_results":null}