# include # include # include # include # include "prime_serial.h" /******************************************************************************/ double cpu_time ( ) /******************************************************************************/ /* Purpose: CPU_TIME returns the current reading on the CPU clock. Discussion: The CPU time measurements available through this routine are often not very accurate. In some cases, the accuracy is no better than a hundredth of a second. Licensing: This code is distributed under the GNU LGPL license. Modified: 06 June 2005 Author: John Burkardt Parameters: Output, double CPU_TIME, the current reading of the CPU clock, in seconds. */ { double value; value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; return value; } /******************************************************************************/ int prime_number ( int n ) /******************************************************************************/ /* 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 GNU LGPL 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; } /******************************************************************************/ 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 GNU LGPL 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 ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }