# include # include # include # include # include "is_prime.h" 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 ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: is_prime_test() tests is_prime(). Licensing: This code is distributed under the MIT license. Modified: 28 December 2022 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "is_prime_test():\n" ); printf ( " C version\n" ); printf ( " Test is_prime()\n" ); is_prime1_test ( ); is_prime2_test ( ); is_prime3_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "is_prime_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void is_prime1_test ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "is_prime1_test():\n" ); printf ( " is_prime1() returns values of the is_prime() function.\n" ); printf ( "\n" ); printf ( " n is_prime(n) is_prime1(n)\n" ); printf ( "\n" ); n_data = 0; while ( true ) { is_prime_values ( &n_data, &n, &tf1 ); if ( n_data == 0 ) { break; } tf2 = is_prime1 ( n ); printf ( "%12d %s %s\n", n, tf1 ? "true" : "false", tf2 ? "true" : "false" ); } return; } /******************************************************************************/ void is_prime2_test ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "is_prime2_test():\n" ); printf ( " is_prime2() returns values of the is_prime() function.\n" ); printf ( "\n" ); printf ( " n is_prime(n) is_prime2(n)\n" ); printf ( "\n" ); n_data = 0; while ( true ) { is_prime_values ( &n_data, &n, &tf1 ); if ( n_data == 0 ) { break; } tf2 = is_prime2 ( n ); printf ( "%12d %s %s\n", n, tf1 ? "true" : "false", tf2 ? "true" : "false" ); } return; } /******************************************************************************/ void is_prime3_test ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "is_prime3_test():\n" ); printf ( " is_prime3() returns values of the is_prime() function.\n" ); printf ( "\n" ); printf ( " n is_prime(n) is_prime3(n)\n" ); printf ( "\n" ); n_data = 0; while ( true ) { is_prime_values ( &n_data, &n, &tf1 ); if ( n_data == 0 ) { break; } tf2 = is_prime3 ( n ); printf ( "%12d %s %s\n", n, tf1 ? "true" : "false", tf2 ? "true" : "false" ); } return; } /******************************************************************************/ void is_prime_values ( int *n_data, int *n, bool *tf ) /******************************************************************************/ /* 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 } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* 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: 24 September 2003 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }