I have this recurrence formula:
P(n) = ( P(n-1) + 2^(n/2) ) % (X)
s.t. P(1) = 2;
where n/2 is computer integer division i.e. floor of x/2
Since i am taking mod X, this relation should repeat at least with in X outputs.
but it can start repeating before that.
How to find this value?
It needn’t repeat within
xterms, considerx = 3:and you see that the period is 4.
Generally (suppose
Xis odd, it’s a bit more involved for evenX), letkbe the period of 2 moduloX, i.e.k > 0,2^k % X = 1, andkis minimal with these properties (see below).Consider all arithmetic modulo
X. ThenIt is easier to see when we separately consider odd and even
n:since each
2^jappears twice, forj = 2*iandj = 2*i+1. For evenn = 2*m, there’s one summand2^mmissing, soand we see that the length of the period is
2*k, since the changing parts2^((n+1)/2)and2^((n+2)/2)have that period. The period immediately begins, there is no pre-period part (there can be a pre-period for evenX).Now
k <= φ(X)by Euler’s generalisation of Fermat’s theorem, so the period is at most2 * φ(X).(φ is Euler’s totient function, i.e.
φ(n)is the number of integers1 <= k <= nwithgcd(n,k) = 1.)What makes it possible that the period is longer than
Xis thatP(n+1)is not completely determined byP(n), the value ofnalso plays a role in determiningP(n+1), in this case the dependence is simple, each power of 2 being used twice in succession doubles the period of the pure powers of 2.Consider the sequence
a[k] = (2^k) % Xfor oddX > 1. It has the simple recurrenceso each value completely determines the next, thus the entire following part of the sequence. (Since
Xis assumed odd, it also determines the previous value [ifk > 0] and thus the entire previous part of the sequence. WithH = (X+1)/2, we havea[k-1] = (H * a[k]) % X.)Hence if the sequence assumes one value twice (and since there are only
Xpossible values, that must happen within the firstX+1values), at indicesiandj = i+p > i, say, the sequence repeats and we havea[k+p] = a[k]for allk >= i. For oddX, we can go back in the sequence, thereforea[k+p] = a[k]also holds for0 <= k < i. Thus the first value that occurs twice in the sequence isa[0] = 1.Let
pbe the smallest positive integer witha[p] = 1. Thenpis the length of the smallest period of the sequencea, anda[k] = 1if and only ifkis a multiple ofp, thus the set of periods ofais the set of multiples ofp. Euler’s theorem says thata[φ(X)] = 1, from that we can conclude thatpis a divisor ofφ(X), in particularp <= φ(X) < X.Now back to the original sequence.
Since each
a[k]is used twice in succession, it is natural to examine the subsequences for even and odd indices separately,then the transition from one value to the next is more regular. For the even indices we find
and for the odd indices
Now if we ignore the modulus for the moment, both
EandOare geometric sums, so there’s an easy closed formula for the terms. They have been given above (in slightly different form),So we see that
Ohas the same (minimal) period asa, namelyp, andEalso has that period. Unless maybe ifXis divisible by 3, that is also the minimal (positive) period ofE(ifXis divisible by 3, the minimal positive period ofEcould be a proper divisor ofp, forX = 3e.g.,Eis constant).Thus we see that
2*pis a period of the sequencePobtained by interlacingEandO.It remains to be seen that
2*pis the minimal positive period ofP. Letmbe the minimal positive period. Thenmis a divisor of2*p.Suppose
mwere odd,m = 2*j+1. Thenand consequently
But
P(2) - P(1) = a[1]andSo we must have
a[1] = a[j+1], hencejis a period ofa, anda[j+1] = a[2*j+2], hencej+1is a period ofatoo. But that means that 1 is a period ofa, which impliesX = 1, a contradiction.Therefore
mis even,m = 2*j. But thenjis a period ofO(and ofE), thus a multiple ofp. On the other hand,m <= 2*pimpliesj <= p, and the only (positive) multiple ofpsatisfying that inequality ispitself, hencej = p,m = 2*p.