# include # include # include # # include "middle_square.h" # /******************************************************************************/ long long int middle_square_next ( long long int s, long long int d ) /******************************************************************************/ /* Purpose: middle_square_next() computes the next middle square random value. Licensing: This code is distributed under the MIT license. Modified: 13 September 2022 Author: John Burkardt Reference: Brian Hayes, The Middle of the Square, 08 August 2022, http://bit-player.org/ Input: long long int s: the current seed, of no more than 2*D digits. long long int d: HALF the number of digits. Typical values are 2, 3, or 4. Output: long long int middle_square, the next seed. */ { long long int divisor; long long int r; /* Square. */ r = s * s; /* Drop last two digits. */ divisor = ( long long int ) ( pow ( 10, d ) ); r = ( r / divisor ); /* Drop all but last four digits. */ divisor = ( long long int ) ( pow ( 10, 2 * d ) ); r = ( r % divisor ); return r; }