# include # include # include # include # # include "middle_square.h" int main ( ); void middle_square_next_test ( ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: middle_square_test() tests middle_square(). Licensing: This code is distributed under the MIT license. Modified: 16 September 2022 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "middle_square_test():\n" ); printf ( " C version\n" ); printf ( " Test middle_square()\n" ); middle_square_next_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "middle_square_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void middle_square_next_test ( ) /******************************************************************************/ /* Purpose: middle_square_next_test() tests middle_square_next(). Discussion: This function simply demonstrates the results of 10 successive calls to middle_square_next(), for a range of values of d. Licensing: This code is distributed under the MIT license. Modified: 16 September 2022 Author: John Burkardt */ { long long int d; long long int divisor; long long int i; long long int s; printf ( "\n" ); printf ( "middle_square_next_test():\n" ); printf ( " middle_square_next ( s, d ) applies the middle square algorithm\n" ); printf ( " using a 2*d digit seed.\n" ); for ( d = 1; d <= 5; d++ ) { printf ( "\n" ); printf ( " Using d = %lld\n", d ); printf ( "\n" ); divisor = ( long long int ) pow ( 10, 2 * d ); s = ( 2147483647 % divisor ); for ( i = 0; i <= 10; i++ ) { printf ( " %2lld %lld\n", i, s ); s = middle_square_next ( s, d ); } } return; } /******************************************************************************/ 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 }