/// Calculate the number of primes below x using the /// Deleglise-Rivat algorithm. /// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space. /// int64_t pi_deleglise_rivat2(int64_t x) { if (x < 2) return 0; double alpha = get_alpha_deleglise_rivat(x); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t c = PhiTiny::get_c(y); int64_t z = x / y; print(""); print("=== pi_deleglise_rivat2(x) ==="); print("pi(x) = S1 + S2 + pi(y) - 1 - P2"); print(x, y, z, c, alpha, 1); int64_t p2 = P2(x, y, 1); int64_t pi_y = pi_legendre(y, 1); int64_t s1 = S1(x, y, c, 1); int64_t s2 = S2(x, y, z, c); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
/// Calculate the number of primes below x using the /// Deleglise-Rivat algorithm. /// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space. /// int64_t pi_deleglise_rivat_parallel2(int64_t x, int threads) { if (x < 2) return 0; double alpha = get_alpha(x, 0.0017154, -0.0508992, 0.483613, 0.0672202); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t z = x / y; int64_t pi_y = pi_legendre(y, 1); int64_t c = PhiTiny::get_c(y); print(""); print("=== pi_deleglise_rivat_parallel2(x) ==="); print("pi(x) = S1 + S2 + pi(y) - 1 - P2"); print(x, y, z, c, alpha, threads); int64_t p2 = P2(x, y, threads); int64_t s1 = S1(x, y, c, threads); int64_t s2_approx = S2_approx(x, pi_y, p2, s1); int64_t s2 = S2(x, y, z, c, s2_approx, threads); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
/// Calculate the number of primes below x using the /// Deleglise-Rivat algorithm. /// Run time: O(x^(2/3) / (log x)^2) operations, O(x^(1/3) * (log x)^3) space. /// int64_t pi_deleglise_rivat1(int64_t x) { if (x < 2) return 0; double alpha = get_alpha_deleglise_rivat(x); int64_t x13 = iroot<3>(x); int64_t y = (int64_t) (x13 * alpha); int64_t z = x / y; int64_t c = PhiTiny::get_c(y); int64_t p2 = P2(x, y, 1); vector<int32_t> mu = generate_moebius(y); vector<int32_t> lpf = generate_least_prime_factors(y); int64_t pi_y = pi_legendre(y, 1); int64_t s1 = S1(x, y, c, 1); int64_t s2 = S2(x, y, z, c, lpf, mu); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }
/// Calculate the number of primes below x using Legendre's formula. /// Run time: O(x) operations, O(x^(1/2)) space. /// int64_t pi_legendre(int64_t x) { return pi_legendre(x, get_num_threads()); }