1 /// 2 /// @file alpha_y.cpp 3 /// @brief Test the alpha_y tuning factor in Gourdon's algorithm. 4 /// y = alpha_y * x^(1/3) 5 /// By computing pi(x) using different alpha_y tuning 6 /// factors we can make sure that all array sizes 7 /// (and other bounds) are accurate. 8 /// 9 /// Copyright (C) 2019 Kim Walisch, <kim.walisch@gmail.com> 10 /// 11 /// This file is distributed under the BSD License. See the COPYING 12 /// file in the top level directory. 13 /// 14 15 #include <gourdon.hpp> 16 #include <primecount.hpp> 17 #include <primecount-internal.hpp> 18 #include <imath.hpp> 19 20 #include <stdint.h> 21 #include <iostream> 22 #include <cstdlib> 23 #include <random> 24 25 using namespace primecount; 26 check(bool OK)27void check(bool OK) 28 { 29 std::cout << " " << (OK ? "OK" : "ERROR") << "\n"; 30 if (!OK) 31 std::exit(1); 32 } 33 main()34int main() 35 { 36 std::random_device rd; 37 std::mt19937 gen(rd()); 38 39 int64_t min = (int64_t) 1e9; 40 int64_t max = min * 2; 41 std::uniform_int_distribution<int64_t> dist(min, max); 42 int threads = get_num_threads(); 43 44 for (int i = 0; i < 20; i++) 45 { 46 int64_t x = dist(gen); 47 int64_t res1 = pi_meissel(x, threads); 48 49 for (double alpha_y = 1; alpha_y <= iroot<6>(x); alpha_y++) 50 { 51 set_alpha_y(alpha_y); 52 int64_t res2 = pi_gourdon(x, threads); 53 54 std::cout << "pi_gourdon(" << x << ") = " << res2; 55 check(res1 == res2); 56 } 57 } 58 59 std::cout << std::endl; 60 std::cout << "All tests passed successfully!" << std::endl; 61 62 return 0; 63 } 64