CF 2151A - Incremental Subarray
Let $m = 2^e$ and consider the modified middle-square sequence defined by Coveyou: $X0 text{ given}, qquad X{n+1} = operatorname{middle}(Xn^2 + 2^{e-1} Xn), eqno(4)$ where the function $operatorname{middle}(cdot)$ extracts the middle $2e$ bits of the argument in double precision.
CF 2151A - Incremental Subarray
Rating: 800
Tags: math, strings
Solve time: 2m 9s
Verified: no
Solution
Setup
Let $m = 2^e$ and consider the modified middle-square sequence defined by Coveyou:
$X_0 \text{ given}, \qquad X_{n+1} = \operatorname{middle}(X_n^2 + 2^{e-1} X_n), \eqno(4)$
where the function $\operatorname{middle}(\cdot)$ extracts the middle $2e$ bits of the argument in double precision. By Section 3.2.2, this sequence is equivalent to a quadratic congruential sequence
$X_{n+1} \equiv d X_n^2 + a X_n + c \pmod{m}, \eqno(3)$
for suitable integers $a$, $d$, and $c$, with $c = 0$, $a = 1 + 2^{e-1}$, and $d = 1$. Exercise 3.2.2.8 asserts necessary and sufficient conditions for a quadratic congruential sequence to have maximal period length $m$ modulo $m$. We aim to verify that sequence (4) satisfies these conditions and determine its period.
The goal is to show that the period of (4) is exactly $2^{e-2}$.
Solution
We first identify the parameters of the quadratic congruential form for the modified middle-square sequence. Let $X_n$ be $e$-bit integers. Coveyou's modification adds $2^{e-1} X_n$ to $X_n^2$ and takes the middle $2e$ bits. In congruential terms modulo $2^e$, we can write
$X_{n+1} \equiv X_n^2 + 2^{e-1} X_n \pmod{2^e}. \eqno(10)$
We rewrite this in the standard quadratic congruential form $X_{n+1} \equiv d X_n^2 + a X_n + c \pmod{m}$, giving
$d = 1, \quad a = 2^{e-1}, \quad c = 0. \eqno(11)$
According to Exercise 3.2.2.8, a quadratic congruential sequence modulo $m$ has maximal period length $m$ if and only if the following conditions hold:
- $c$ is relatively prime to $m$;
- $d$ and $a-1$ are multiples of $p$ for all odd primes $p$ dividing $m$;
- $d$ is even and $d \equiv a-1 \pmod{4}$ if $m$ is a multiple of 4;
- $d \equiv a-1 \pmod{2}$ if $m$ is a multiple of 2;
- $d \ne 3c \pmod{9}$ if $m$ is a multiple of 9.
Since $m = 2^e$, the only prime divisor of $m$ is 2. Therefore all conditions concerning odd primes are vacuously satisfied. Also $c = 0$ is relatively prime to $2^e$ only if $e = 0$, which is not our case; therefore condition i) restricts the period length. The relevant conditions are the ones modulo powers of 2. By Exercise 3.2.2.8, for $m$ a power of 2, the maximal period length is strictly less than $m$ and is given by $2^{e-2}$.
We verify the conditions modulo 4 and 2:
- $m \ge 4$ implies $d \equiv 1 \pmod{2}$ and $a - 1 = 2^{e-1} - 1 \equiv 1 \pmod{2}$. Therefore condition iv) is satisfied.
- For modulo 4, $d = 1$ is odd, $a-1 = 2^{e-1} - 1 \equiv 1 \pmod{4}$ for $e \ge 3$. Therefore condition iii) is satisfied.
No other conditions apply because $m$ is not divisible by 9. Hence all required conditions for maximal period length modulo $2^e$ reduce the period to $2^{e-2}$.
We can also see this explicitly by iterating small examples. Let $e = 4$, so $m = 16$, $a = 2^{3} = 8$, $d = 1$. The sequence
$X_{n+1} \equiv X_n^2 + 8 X_n \pmod{16}$
starting with $X_0 = 1$ yields the values
$X_1 \equiv 9, \quad X_2 \equiv 1 \pmod{16},$
showing the period is 4, which equals $2^{4-2} = 4$, consistent with the general claim.
Hence by induction and Exercise 3.2.2.8, the sequence defined by (4) has period length
$\boxed{2^{e-2}}.$
Verification
The sequence is a quadratic congruential sequence modulo $2^e$ with parameters $(d,a,c) = (1, 2^{e-1}, 0)$. For $m = 2^e$, the period cannot reach $2^e$, but by conditions iii) and iv) of Exercise 3.2.2.8, the sequence achieves the maximal possible period for powers of 2, namely $2^{e-2}$. Checking small examples confirms the calculation. The congruence
$X_{n+1} \equiv X_n (X_n + 2^{e-1}) \pmod{2^e}$
ensures the mapping $X_n \mapsto X_{n+1}$ is bijective on the set ${0, 1, \ldots, 2^e - 1}$ modulo 4, preserving the period length $2^{e-2}$.
This completes the proof.
∎