# include # include # include # include using namespace std; # include "prime.hpp" //****************************************************************************80 double cpu_time ( ) //****************************************************************************80 // // Purpose: // // CPU_TIME reports the elapsed CPU time. // // Discussion: // // The data available to this routine through "CLOCK" is not very reliable, // and hence the values of CPU_TIME returned should not be taken too // seriously, especially when short intervals are being timed. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 September 2008 // // Author: // // John Burkardt // // Parameters: // // Output, double CPU_TIME, the current total elapsed CPU time in second. // { double value; value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; return value; } //****************************************************************************80 int prime_number ( int n ) //****************************************************************************80 // // Purpose: // // PRIME_NUMBER returns the number of primes between 1 and N. // // Discussion: // // A naive algorithm is used. // // Mathematica can return the number of primes less than or equal to N // by the command PrimePi[N]. // // N PRIME_NUMBER // // 1 0 // 10 4 // 100 25 // 1,000 168 // 10,000 1,229 // 100,000 9,592 // 1,000,000 78,498 // 10,000,000 664,579 // 100,000,000 5,761,455 // 1,000,000,000 50,847,534 // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 23 April 2009 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the maximum number to check. // // Output, int PRIME_NUMBER, the number of prime numbers up to N. // { int i; int j; int prime; int total; total = 0; for ( i = 2; i <= n; i++ ) { prime = 1; for ( j = 2; j < i; j++ ) { if ( ( i % j ) == 0 ) { prime = 0; break; } } total = total + prime; } return total; } //****************************************************************************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: // // 24 September 2003 // // Author: // // John Burkardt // // Parameters: // // None // { # 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 ); cout << time_buffer << "\n"; return; # undef TIME_SIZE }