Created
January 3, 2026 21:21
-
-
Save Hermann-SW/459c5a4c59d7a810a4d7bdea4078db1f to your computer and use it in GitHub Desktop.
Determine pi(10^n) = pi_{k}(10^n) (for all k<=10^n)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // NOLINT(legal/copyright) | |
| // g++ pi.cc -lgmp -lgmpxx -O3 -Wall -pedantic -Wextra -o pi | |
| // (cpplinted and cppchecked) | |
| // | |
| #include <time.h> | |
| #include <math.h> | |
| #include <gmpxx.h> | |
| #include <assert.h> | |
| #include <iostream> | |
| #include <cstdint> | |
| int main(int argc, char *argv[]) { | |
| mpz_class mx, a = 2; | |
| if (argc < 2) { | |
| std::cerr << "Format: ./pi exp\n"; | |
| exit(1); | |
| } | |
| uint64_t c = 0, u = atoi(argv[1]); | |
| mpz_ui_pow_ui(mx.get_mpz_t(), 10, u); | |
| do { | |
| mpz_nextprime(a.get_mpz_t(), a.get_mpz_t()); | |
| ++c; | |
| } | |
| while (a <= mx); // NOLINT | |
| std::cerr << "pi(" << mx << ")=" << c << "\n"; | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above gist code is outperformed by simple sieve of Eratosthenes.
But GMP code does not need big memory, while sieve code does.