# include # include # include # include using namespace std; # include "is_prime.hpp" int main ( ); void is_prime1_test ( ); void is_prime2_test ( ); void is_prime3_test ( ); void is_prime_values ( int &n_data, int &n, bool &tf ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // is_prime_test() tests is_prime(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 28 December 2022 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "is_prime_test():\n"; cout << " C++ version\n"; cout << " Test is_prime()\n"; is_prime1_test ( ); is_prime2_test ( ); is_prime3_test ( ); /* Terminate. */ cout << "\n"; cout << "is_prime_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void is_prime1_test ( ) //****************************************************************************80 // // Purpose: // // is_prime1_test() tests is_prime1(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 28 December 2022 // // Author: // // John Burkardt // { int n; int n_data; bool tf1; bool tf2; cout << "\n"; cout << "is_prime1_test():\n"; cout << " is_prime1() returns values of the is_prime() function.\n"; cout << "\n"; cout << " n is_prime(n) is_prime1(n)\n"; cout << "\n"; n_data = 0; while ( true ) { is_prime_values ( n_data, n, tf1 ); if ( n_data == 0 ) { break; } tf2 = is_prime1 ( n ); cout << " " << setw(12) << n << " " << ( tf1 ? "true" : "false" ) << " " << ( tf2 ? "true" : "false" ) << "\n"; } return; } //****************************************************************************80 void is_prime2_test ( ) //****************************************************************************80 // // Purpose: // // is_prime2_test() tests is_prime2(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 28 December 2022 // // Author: // // John Burkardt // { int n; int n_data; bool tf1; bool tf2; cout << "\n"; cout << "is_prime2_test():\n"; cout << " is_prime2() returns values of the is_prime() function.\n"; cout << "\n"; cout << " n is_prime(n) is_prime2(n)\n"; cout << "\n"; n_data = 0; while ( true ) { is_prime_values ( n_data, n, tf1 ); if ( n_data == 0 ) { break; } tf2 = is_prime2 ( n ); cout << " " << setw(12) << n << " " << ( tf1 ? "true" : "false" ) << " " << ( tf2 ? "true" : "false" ) << "\n"; } return; } //****************************************************************************80 void is_prime3_test ( ) //****************************************************************************80 // // Purpose: // // is_prime3_test() tests is_prime3(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 28 December 2022 // // Author: // // John Burkardt // { int n; int n_data; bool tf1; bool tf2; cout << "\n"; cout << "is_prime3_test():\n"; cout << " is_prime3() returns values of the is_prime() function.\n"; cout << "\n"; cout << " n is_prime(n) is_prime3(n)\n"; cout << "\n"; n_data = 0; while ( true ) { is_prime_values ( n_data, n, tf1 ); if ( n_data == 0 ) { break; } tf2 = is_prime3 ( n ); cout << " " << setw(12) << n << " " << ( tf1 ? "true" : "false" ) << " " << ( tf2 ? "true" : "false" ) << "\n"; } return; } //****************************************************************************80 void is_prime_values ( int &n_data, int &n, bool &tf ) //****************************************************************************80 // // Purpose: // // is_prime_values() returns values of the is_prime() function. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 28 December 2022 // // Author: // // John Burkardt // // 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: a positive integer. // // bool tf: true if n is a prime. // { # define N_MAX 22 static int n_vec[N_MAX] = { 1, 2, 12, 3, 91, 53, 437, 311, 1333, 719, 16483, 7919, 223609, 81799, 873599, 800573, 5693761, 7559173, 90166053, 69600977, 6110601, 145253029 }; static int tf_vec[N_MAX] = { false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true }; if ( n_data < 0 ) { n_data = 0; } n_data = n_data + 1; if ( N_MAX < n_data ) { n_data = 0; n = 0; tf = false; } else { n = n_vec[n_data-1]; tf = tf_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 }