# include # include # include # include using namespace std; # include "prime_pi.hpp" int main ( ); void prime_pi1_test ( ); void pi_values ( int &n_data, int &n, int &p ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // prime_pi_test() tests prime_pi(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 31 December 2022 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "prime_pi_test():\n"; cout << " C++ version\n"; cout << " Test prime_pi()\n"; prime_pi1_test ( ); /* Terminate. */ cout << "\n"; cout << "prime_pi_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void prime_pi1_test ( ) //****************************************************************************80 /* Purpose: prime_pi1_test() tests prime_pi1(). Licensing: This code is distributed under the MIT license. Modified: 31 December 2022 Author: John Burkardt */ { int n; int n_data; int pi1; int pi2; cout << "\n"; cout << "prime_pi1_test():\n"; cout << " Test prime_pi1()\n"; cout << " n Pi(n) prime_pi1(n)\n"; cout << "\n"; n_data = 0; while ( true ) { pi_values ( n_data, n, pi1 ); if ( n_data == 0 ) { break; } pi2 = prime_pi1 ( n ); cout << setw(12) << n << setw(10) << pi1 << setw(10) << pi2 << "\n"; } return; } //****************************************************************************80 void pi_values ( int &n_data, int &n, int &p ) //****************************************************************************80 // // Purpose: // // pi_values() returns values of the Pi function. // // Discussion: // // Pi[n] is the number of primes less than or equal to n. // // In Mathematica, the function can be evaluated by: // // PrimePi[n] // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 31 December 2022 // // Author: // // John Burkardt // // Reference: // // Stephen Wolfram, // The Mathematica Book, // Fourth Edition, // Cambridge University Press, 1999, // ISBN: 0-521-64314-7, // LC: QA76.95.W65. // // Input: // // int &N_DATA. The user sets N_DATA to 0 before the first call. // // Output: // // int &N_DATA. On each call, the routine increments N_DATA by 1, and // returns the corresponding data; when there is no more data, the // output value of N_DATA will be 0 again. // // int &N, the argument. // // int &P, the value of the function. // { # define N_MAX 21 static int n_vec[N_MAX] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576 }; static int p_vec[N_MAX] = { 0, 1, 2, 4, 6, 11, 18, 31, 54, 97, 172, 309, 564, 1028, 1900, 3512, 6542, 12251, 23000, 43390, 82025 }; if ( n_data < 0 ) { n_data = 0; } n_data = n_data + 1; if ( N_MAX < n_data ) { n_data = 0; n = 0; p = 0; } else { n = n_vec[n_data-1]; p = p_vec[n_data-1]; } return; # undef N_MAX } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }