# include # include # include # include # include "prime_pi.h" int main ( ); void prime_pi1_test ( ); void pi_values ( int *n_data, int *n, int *p ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: prime_pi_test() tests prime_pi(). Licensing: This code is distributed under the MIT license. Modified: 31 December 2022 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "prime_pi_test():\n" ); printf ( " C version\n" ); printf ( " Test prime_pi()\n" ); prime_pi1_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "prime_pi_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void prime_pi1_test ( ) /******************************************************************************/ /* 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; printf ( "\n" ); printf ( "prime_pi1_test():\n" ); printf ( " Test prime_pi1()\n" ); printf ( " n Pi(n) prime_pi1(n)\n" ); printf ( "\n" ); n_data = 0; while ( true ) { pi_values ( &n_data, &n, &pi1 ); if ( n_data == 0 ) { break; } pi2 = prime_pi1 ( n ); printf ( "%12d %10d %10d\n", n, pi1, pi2 ); } return; } /******************************************************************************/ void pi_values ( int *n_data, int *n, int *p ) /******************************************************************************/ /* 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. 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 } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 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 }