{"id":16315373,"url":"https://github.com/kimwalisch/primecount","last_synced_at":"2025-05-15T19:06:32.904Z","repository":{"id":2108775,"uuid":"10589177","full_name":"kimwalisch/primecount","owner":"kimwalisch","description":"🚀 Fast prime counting function library","archived":false,"fork":false,"pushed_at":"2025-05-14T17:41:54.000Z","size":9189,"stargazers_count":327,"open_issues_count":2,"forks_count":41,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-05-14T18:49:22.582Z","etag":null,"topics":["arm-sve","avx512","math","number-theory","openmp","prime-counting-function","prime-numbers","primecount","primepi","primes"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kimwalisch.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog","contributing":null,"funding":".github/FUNDING.yml","license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"kimwalisch","custom":"https://paypal.com/donate?hosted_button_id=YUZZ2L6FE4ZNW"}},"created_at":"2013-06-09T21:03:30.000Z","updated_at":"2025-05-14T17:41:57.000Z","dependencies_parsed_at":"2023-07-05T18:47:53.855Z","dependency_job_id":"35043713-aee7-40fd-9e48-e33783325b7e","html_url":"https://github.com/kimwalisch/primecount","commit_stats":{"total_commits":5273,"total_committers":5,"mean_commits":1054.6,"dds":"0.010999431063910436","last_synced_commit":"fa5b042ac65ee8672d3fe586319ecfe5b2149882"},"previous_names":[],"tags_count":93,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimwalisch%2Fprimecount","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimwalisch%2Fprimecount/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimwalisch%2Fprimecount/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kimwalisch%2Fprimecount/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kimwalisch","download_url":"https://codeload.github.com/kimwalisch/primecount/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209251,"owners_count":22032887,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm-sve","avx512","math","number-theory","openmp","prime-counting-function","prime-numbers","primecount","primepi","primes"],"created_at":"2024-10-10T21:57:18.721Z","updated_at":"2025-05-15T19:06:32.889Z","avatar_url":"https://github.com/kimwalisch.png","language":"C++","funding_links":["https://github.com/sponsors/kimwalisch","https://paypal.com/donate?hosted_button_id=YUZZ2L6FE4ZNW","https://github.com/sponsors/kimwalisch)!"],"categories":["C++"],"sub_categories":[],"readme":"# primecount\n\n[![Build status](https://github.com/kimwalisch/primecount/actions/workflows/ci.yml/badge.svg)](https://github.com/kimwalisch/primecount/actions/workflows/ci.yml) [![Build status](https://github.com/kimwalisch/primecount/actions/workflows/benchmark.yml/badge.svg)](https://github.com/kimwalisch/primecount/actions/workflows/benchmark.yml)\n[![Github Releases](https://img.shields.io/github/release/kimwalisch/primecount.svg)](https://github.com/kimwalisch/primecount/releases)\n[![C API Documentation](https://img.shields.io/badge/docs-C_API-blue)](doc/libprimecount.md)\n[![C++ API Documentation](https://img.shields.io/badge/docs-C++_API-blue)](doc/libprimecount.md)\n\nprimecount is a command-line program and C/C++ library that counts the number of\nprimes\u0026nbsp;≤\u0026nbsp;x (maximum 10\u003csup\u003e31\u003c/sup\u003e) using **highly optimized** implementations of the combinatorial\n[prime counting algorithms](https://en.wikipedia.org/wiki/Prime-counting_function#Algorithms_for_evaluating_%CF%80(x)).\n\nprimecount includes implementations of all important combinatorial prime counting algorithms\nknown up to this date all of which have been parallelized using\n[OpenMP](https://en.wikipedia.org/wiki/OpenMP). primecount contains the first ever open\nsource implementations of the Deleglise-Rivat algorithm and Xavier Gourdon's algorithm (that works).\nprimecount also features a [novel load balancer](https://github.com/kimwalisch/primecount/blob/master/src/LoadBalancerS2.cpp)\nthat is shared amongst all implementations and that scales up to hundreds of CPU cores. primecount\nhas already been used to compute several prime counting function [world records](doc/Records.md).\n\n## Installation\n\nThe primecount command-line program is available in a few package managers.\nFor doing development with libprimecount you may need to install\n```libprimecount-dev``` or ```libprimecount-devel```.\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eWindows:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003ewinget install primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003emacOS:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003ebrew install primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eArch Linux:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003esudo pacman -S primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eDebian/Ubuntu:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003esudo apt install primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eFedora:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003esudo dnf install primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eFreeBSD:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003epkg install primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eopenSUSE:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ccode\u003esudo zypper install primecount\u003c/code\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n## Build instructions\n\nYou need to have installed a C++ compiler and CMake. Ideally\nprimecount should be compiled using GCC or Clang as these compilers\nsupport both OpenMP (multi-threading library) and 128-bit integers.\n\n```sh\ncmake .\ncmake --build . --parallel\nsudo cmake --install .\nsudo ldconfig\n```\n\n* [Detailed build instructions](doc/BUILD.md)\n\n## Usage examples\n\n```sh\n# Count the primes ≤ 10^14\nprimecount 1e14\n\n# Print progress and status information during computation\nprimecount 1e20 --status\n\n# Count primes using Meissel's algorithm\nprimecount 2**32 --meissel\n\n# Find the 10^14th prime using 4 threads\nprimecount 1e14 --nth-prime --threads=4 --time\n```\n\n## Command-line options\n\n```\nUsage: primecount x [options]\nCount the number of primes less than or equal to x (\u003c= 10^31).\n\nOptions:\n\n  -d, --deleglise-rivat    Count primes using the Deleglise-Rivat algorithm\n  -g, --gourdon            Count primes using Xavier Gourdon's algorithm.\n                           This is the default algorithm.\n  -l, --legendre           Count primes using Legendre's formula\n      --lehmer             Count primes using Lehmer's formula\n      --lmo                Count primes using Lagarias-Miller-Odlyzko\n  -m, --meissel            Count primes using Meissel's formula\n      --Li                 Eulerian logarithmic integral function\n      --Li-inverse         Approximate the nth prime using Li^-1(x)\n  -n, --nth-prime          Calculate the nth prime\n  -p, --primesieve         Count primes using the sieve of Eratosthenes\n      --phi \u003cX\u003e \u003cA\u003e        phi(x, a) counts the numbers \u003c= x that are not\n                           divisible by any of the first a primes\n  -R, --RiemannR           Approximate pi(x) using the Riemann R function\n      --RiemannR-inverse   Approximate the nth prime using R^-1(x)\n  -s, --status[=NUM]       Show computation progress 1%, 2%, 3%, ...\n                           Set digits after decimal point: -s1 prints 99.9%\n      --test               Run various correctness tests and exit\n      --time               Print the time elapsed in seconds\n  -t, --threads=NUM        Set the number of threads, 1 \u003c= NUM \u003c= CPU cores.\n                           By default primecount uses all available CPU cores.\n  -v, --version            Print version and license information\n  -h, --help               Print this help menu\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eAdvanced options\u003c/summary\u003e\n\n```\nAdvanced options for the Deleglise-Rivat algorithm:\n\n  -a, --alpha=NUM          Set tuning factor: y = x^(1/3) * alpha\n      --P2                 Compute the 2nd partial sieve function\n      --S1                 Compute the ordinary leaves\n      --S2-trivial         Compute the trivial special leaves\n      --S2-easy            Compute the easy special leaves\n      --S2-hard            Compute the hard special leaves\n\nAdvanced options for Xavier Gourdon's algorithm:\n\n      --alpha-y=NUM        Set tuning factor: y = x^(1/3) * alpha_y\n      --alpha-z=NUM        Set tuning factor: z = y * alpha_z\n      --AC                 Compute the A + C formulas\n      --B                  Compute the B formula\n      --D                  Compute the D formula\n      --Phi0               Compute the Phi0 formula\n      --Sigma              Compute the 7 Sigma formulas\n```\n\n\u003c/details\u003e\n\n## Benchmarks\n\n\u003ctable\u003e\n  \u003ctr align=\"center\"\u003e\n    \u003ctd\u003e\u003cb\u003ex\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003cb\u003ePrime Count\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003cb\u003eLegendre\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003cb\u003eMeissel\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003cb\u003eLagarias\u003cbr/\u003eMiller\u003cbr/\u003eOdlyzko\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003cb\u003eDeleglise\u003cbr/\u003eRivat\u003c/b\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003cb\u003eGourdon\u003c/b\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e10\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e455,052,511\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.00s\u003c/td\u003e\n    \u003ctd\u003e0.00s\u003c/td\u003e\n    \u003ctd\u003e0.00s\u003c/td\u003e\n    \u003ctd\u003e0.00s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e11\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e4,118,054,813\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.00s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e12\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e37,607,912,018\u003c/td\u003e\n    \u003ctd\u003e0.02s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e13\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e346,065,536,839\u003c/td\u003e\n    \u003ctd\u003e0.03s\u003c/td\u003e\n    \u003ctd\u003e0.02s\u003c/td\u003e\n    \u003ctd\u003e0.02s\u003c/td\u003e\n    \u003ctd\u003e0.02s\u003c/td\u003e\n    \u003ctd\u003e0.01s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e14\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e3,204,941,750,802\u003c/td\u003e\n    \u003ctd\u003e0.11s\u003c/td\u003e\n    \u003ctd\u003e0.05s\u003c/td\u003e\n    \u003ctd\u003e0.03s\u003c/td\u003e\n    \u003ctd\u003e0.03s\u003c/td\u003e\n    \u003ctd\u003e0.02s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e15\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e29,844,570,422,669\u003c/td\u003e\n    \u003ctd\u003e0.45s\u003c/td\u003e\n    \u003ctd\u003e0.21s\u003c/td\u003e\n    \u003ctd\u003e0.14s\u003c/td\u003e\n    \u003ctd\u003e0.13s\u003c/td\u003e\n    \u003ctd\u003e0.06s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e16\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e279,238,341,033,925\u003c/td\u003e\n    \u003ctd\u003e3.09s\u003c/td\u003e\n    \u003ctd\u003e1.12s\u003c/td\u003e\n    \u003ctd\u003e0.41s\u003c/td\u003e\n    \u003ctd\u003e0.31s\u003c/td\u003e\n    \u003ctd\u003e0.20s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e17\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e2,623,557,157,654,233\u003c/td\u003e\n    \u003ctd\u003e25.28s\u003c/td\u003e\n    \u003ctd\u003e8.84s\u003c/td\u003e\n    \u003ctd\u003e1.81s\u003c/td\u003e\n    \u003ctd\u003e1.27s\u003c/td\u003e\n    \u003ctd\u003e0.51s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e18\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e24,739,954,287,740,860\u003c/td\u003e\n    \u003ctd\u003e214.63s\u003c/td\u003e\n    \u003ctd\u003e78.00s\u003c/td\u003e\n    \u003ctd\u003e8.18s\u003c/td\u003e\n    \u003ctd\u003e5.33s\u003c/td\u003e\n    \u003ctd\u003e2.00s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e19\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e234,057,667,276,344,607\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003e24.40s\u003c/td\u003e\n    \u003ctd\u003e8.12s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e20\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e2,220,819,602,560,918,840\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003e113.60s\u003c/td\u003e\n    \u003ctd\u003e32.87s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e21\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e21,127,269,486,018,731,928\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003e500.51s\u003c/td\u003e\n    \u003ctd\u003e134.21s\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr align=\"right\"\u003e\n    \u003ctd\u003e10\u003csup\u003e22\u003c/sup\u003e\u003c/td\u003e\n    \u003ctd\u003e201,467,286,689,315,906,290\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003eNaN\u003c/td\u003e\n    \u003ctd\u003e2,198.92s\u003c/td\u003e\n    \u003ctd\u003e552.17s\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nThe benchmarks above were run on an AMD EPYC Zen4 CPU from 2023 with 32 CPU cores (no Hyper-Threading)\nclocked at 3.7 GHz. Note that Jan Büthe mentions in \u003ca href=\"doc/References.md\"\u003e[11]\u003c/a\u003e\nthat he computed $\\pi(10^{25})$ in 40,000 CPU core hours using the analytic prime\ncounting function algorithm. Büthe also mentions that by using additional zeros of the\nzeta function the runtime could have potentially been reduced to 4,000 CPU core hours.\nHowever using primecount and Xavier Gourdon's algorithm $\\pi(10^{25})$ can be computed\nin only 380 CPU core hours on the AMD EPYC Zen4 CPU (from 2023)!\n\n## Algorithms\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eLegendre's Formula\u003c/td\u003e\n    \u003ctd\u003e$\\pi(x)=\\pi(\\sqrt{x})+\\phi(x,\\pi(\\sqrt{x}))-1$\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eMeissel's Formula\u003c/td\u003e\n    \u003ctd\u003e$\\pi(x)=\\pi(\\sqrt[3]{x})+\\phi(x,\\pi(\\sqrt[3]{x}))-\\mathrm{P_2}(x,\\pi(\\sqrt[3]{x}))-1$\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eLehmer's Formula\u003c/td\u003e\n    \u003ctd\u003e$\\pi(x)=\\pi(\\sqrt[4]{x})+\\phi(x,\\pi(\\sqrt[4]{x}))-\\mathrm{P_2}(x,\\pi(\\sqrt[4]{x}))-\\mathrm{P_3}(x,\\pi(\\sqrt[4]{x}))-1$\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eLMO Formula\u003c/td\u003e\n    \u003ctd\u003e$\\pi(x)=\\pi(\\sqrt[3]{x})+\\mathrm{S_1}(x,\\pi(\\sqrt[3]{x}))+\\mathrm{S_2}(x,\\pi(\\sqrt[3]{x}))-\\mathrm{P_2}(x,\\pi(\\sqrt[3]{x}))-1$\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nUp until the early 19th century the most efficient known method for counting primes was the\nsieve of Eratosthenes which has a running time of $O(x\\ \\log\\ \\log\\ x)$ operations. The first\nimprovement to this bound was Legendre's formula (1830) which uses the inclusion-exclusion\nprinciple to calculate the number of primes below x without enumerating the individual\nprimes. Legendre's formula has a running time of $O(x)$ operations and uses $O(\\sqrt{x}/\\log{x})$\nspace. In 1870 E. D. F. Meissel improved Legendre's formula by setting $a=\\pi(\\sqrt[3]{x})$\nand by adding the correction term $\\mathrm{P_2}(x,a)$, Meissel's formula has a running time\nof $O(x/\\log^3{x})$ operations and uses $O(\\sqrt[3]{x})$ space. In 1959 D. H. Lehmer\nextended Meissel's formula and slightly improved the running time to $O(x/\\log^4{x})$\noperations and $O(x^{\\frac{3}{8}})$ space. In 1985 J. C. Lagarias, V. S. Miller and A. M.\nOdlyzko published a new algorithm based on Meissel's formula which has a lower runtime\ncomplexity of $O(x^{\\frac{2}{3}}/\\log{x})$ operations and which uses only\n$O(\\sqrt[3]{x}\\ \\log^2{x})$ space.\n\nprimecount's Legendre, Meissel and Lehmer implementations are based\non Hans Riesel's book \u003ca href=\"doc/References.md\"\u003e[5]\u003c/a\u003e,\nits Lagarias-Miller-Odlyzko and Deleglise-Rivat implementations are\nbased on Tomás Oliveira's paper \u003ca href=\"doc/References.md\"\u003e[9]\u003c/a\u003e\nand the implementation of Xavier Gourdon's algorithm is based\non Xavier Gourdon's paper \u003ca href=\"doc/References.md\"\u003e[7]\u003c/a\u003e.\nprimecount's implementation of the so-called hard special leaves is different\nfrom the algorithms that have been described in any of the combinatorial\nprime counting papers so far. Instead of using a binary indexed tree\nfor counting which is very cache inefficient primecount uses a linear\ncounter array in combination with the POPCNT instruction which is more\ncache efficient and much faster. The\n[Hard-Special-Leaves.md](doc/Hard-Special-Leaves.md) document contains more\ninformation. primecount's [easy special leaf implementation](doc/Easy-Special-Leaves.md)\nand its [partial sieve function implementation](doc/Partial-Sieve-Function.md)\nalso contain significant improvements.\n\n## Fast nth prime calculation\n\nThe most efficient known method for calculating the nth prime is a combination\nof the prime counting function and a prime sieve. The idea is to closely\napproximate the nth prime e.g. using the inverse logarithmic integral\n$\\mathrm{Li}^{-1}(n)$ or the inverse Riemann R function $\\mathrm{R}^{-1}(n)$\nand then count the primes up to this guess using the prime counting function.\nOnce this is done one starts sieving (e.g. using the segmented sieve of\nEratosthenes) from there on until one finds the actual nth prime. The author\nhas implemented ```primecount::nth_prime(n)``` this way\n(option: ```--nth-prime```), it finds the nth prime in $O(x^{\\frac{2}{3}}/\\log^2{x})$\noperations using $O(\\sqrt{x})$ space.\n\n## C API\n\nInclude the ```\u003cprimecount.h\u003e``` header to use primecount's C API.\nAll functions that are part of primecount's C API return ```-1``` in case an\nerror occurs and print the corresponding error message to the standard error\nstream.\n\n```C\n#include \u003cprimecount.h\u003e\n#include \u003cstdio.h\u003e\n\nint main()\n{\n    int64_t pix = primecount_pi(1000);\n    printf(\"primes \u003c= 1000: %ld\\n\", pix);\n\n    return 0;\n}\n```\n\n* [C API documentation](doc/libprimecount.md#libprimecount)\n* [libprimecount build instructions](doc/libprimecount.md#build-instructions)\n\n## C++ API\n\nInclude the ```\u003cprimecount.hpp\u003e``` header to use primecount's C++ API.\nAll functions that are part of primecount's C++ API throw a\n```primecount_error``` exception (which is derived from\n```std::exception```) in case an error occurs.\n\n```C++\n#include \u003cprimecount.hpp\u003e\n#include \u003ciostream\u003e\n\nint main()\n{\n    int64_t pix = primecount::pi(1000);\n    std::cout \u003c\u003c \"primes \u003c= 1000: \" \u003c\u003c pix \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\n* [C++ API documentation](doc/libprimecount.md#libprimecount)\n* [libprimecount build instructions](doc/libprimecount.md#build-instructions)\n\n## Bindings for other languages\n\nprimesieve natively supports C and C++ and has bindings available for:\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eCommon Lisp:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/AaronChen0/cl-primecount\"\u003ecl-primecount\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eJulia:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/JuliaBinaryWrappers/primecount_jll.jl\"\u003eprimecount_jll.jl\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eLua:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/ishandutta2007/lua-primecount\"\u003elua-primecount\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eHaskell:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/pgujjula/primecount-haskell\"\u003eprimecount-haskell\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003ePython:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/dimpase/primecountpy\"\u003eprimecountpy\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003ePython:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/hearot/primecount-python\"\u003eprimecount-python\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003e\u003cb\u003eRust:\u003c/b\u003e\u003c/td\u003e\n        \u003ctd\u003e\u003ca href=\"https://github.com/maitbayev/primecount-rs\"\u003eprimecount-rs\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\nMany thanks to the developers of these bindings!\n\n## Sponsors\n\nThanks to all current and past [sponsors of primecount](https://github.com/sponsors/kimwalisch)! Your donations help me purchase (or rent) the latest CPUs and ensure primecount runs at maximum performance on them. Your donations also motivate me to continue maintaining primecount.\n\n\u003ca href=\"https://github.com/AndrewVSutherland\"\u003e\u003cimg src=\"https://images.weserv.nl/?url=avatars.githubusercontent.com/u/11425002?h=60\u0026w=60\u0026fit=cover\u0026mask=circle\"\u003e\u003c/img\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/wolframresearch\"\u003e\u003cimg src=\"https://images.weserv.nl/?url=avatars.githubusercontent.com/u/11549616?h=60\u0026w=60\u0026fit=cover\u0026mask=circle\"\u003e\u003c/img\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/AlgoWin\"\u003e\u003cimg src=\"https://images.weserv.nl/?url=avatars.githubusercontent.com/u/44401099?h=60\u0026w=60\u0026fit=cover\u0026mask=circle\"\u003e\u003c/img\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/sethtroisi\"\u003e\u003cimg src=\"https://images.weserv.nl/?url=avatars.githubusercontent.com/u/10172976?h=60\u0026w=60\u0026fit=cover\u0026mask=circle\"\u003e\u003c/img\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/entersoftone\"\u003e\u003cimg src=\"https://images.weserv.nl/?url=avatars.githubusercontent.com/u/80900902?h=60\u0026w=60\u0026fit=cover\u0026mask=circle\"\u003e\u003c/img\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/utmcontent\"\u003e\u003cimg src=\"https://images.weserv.nl/?url=avatars.githubusercontent.com/u/4705133?h=60\u0026w=60\u0026fit=cover\u0026mask=circle\"\u003e\u003c/img\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimwalisch%2Fprimecount","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkimwalisch%2Fprimecount","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkimwalisch%2Fprimecount/lists"}