/// 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_deleglise_rivat(x); 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, threads); 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; }
int256_t P2(int128_t x, int threads) { if (x < 1) return 0; double alpha = get_alpha_deleglise_rivat(x); string limit = get_max_x(alpha); if (x > to_int128(limit)) throw primesum_error("P2(x): x must be <= " + limit); if (is_print()) set_print_variables(true); int64_t y = (int64_t) (iroot<3>(x) * alpha); return P2(x, y, threads); }
int256_t S2_hard(int128_t x, int threads) { if (x < 1) return 0; double alpha = get_alpha_deleglise_rivat(x); string limit = get_max_x(alpha); if (x > to_int128(limit)) throw primesum_error("S2_hard(x): x must be <= " + limit); if (is_print()) set_print_variables(true); int64_t y = (int64_t) (iroot<3>(x) * alpha); int64_t z = (int64_t) (x / y); int64_t c = PhiTiny::get_c(y); return S2_hard(x, y, z, c, threads); }
maxint_t P2(maxint_t x, int threads) { if (x < 1) return 0; double alpha = get_alpha_deleglise_rivat(x); string limit = get_max_x(alpha); if (x > to_maxint(limit)) throw primecount_error("P2(x): x must be <= " + limit); if (is_print()) set_print_variables(true); int64_t y = (int64_t) (iroot<3>(x) * alpha); if (x <= numeric_limits<int64_t>::max()) return P2((int64_t) x, y, threads); else return P2(x, y, threads); }
/// 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; }
maxint_t S2_hard(maxint_t x, int threads) { if (x < 1) return 0; double alpha = get_alpha_deleglise_rivat(x); string limit = get_max_x(alpha); if (x > to_maxint(limit)) throw primecount_error("S2_hard(x): x must be <= " + limit); if (is_print()) set_print_variables(true); int64_t y = (int64_t) (iroot<3>(x) * alpha); int64_t z = (int64_t) (x / y); int64_t c = PhiTiny::get_c(y); if (x <= numeric_limits<int64_t>::max()) return S2_hard((int64_t) x, y, z, c, (int64_t) Ri(x), threads); else return S2_hard(x, y, z, c, Ri(x), threads); }
/// 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_parallel1(int64_t x, int threads) { 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 p2 = P2(x, y, threads); vector<int32_t> mu = generate_moebius(y); vector<int32_t> lpf = generate_least_prime_factors(y); vector<int32_t> primes = generate_primes(y); int64_t pi_y = pi_bsearch(primes, y); int64_t c = PhiTiny::get_c(y); int64_t s1 = S1(x, y, c, threads); int64_t s2 = S2(x, y, z, c, primes, lpf, mu, threads); int64_t phi = s1 + s2; int64_t sum = phi + pi_y - 1 - p2; return sum; }