3 ms·
> In practice, that loop seems to grow in size by a factor of 5 each time. (I assume there is a number theoretic reason for this, but I am not sure because I am
by waterhouse 16y ago
> In practice, that loop seems to grow in size by a factor of 5 each time. (I assume there is a number theoretic reason for this, but I am not sure because I am terrible at number theory.)
I can explain this reason. Consider 2^n mod 10^k. If n > k, then we can rewrite this as:
2^k * (2^(n-k) mod 5^k)
And then the Euler-Fermat theorem states that, if a and m are coprime, then
a^(φ(m)) ≣ 1 (mod m)
where φ(m) is Euler's totient function, the number of integers from 1 to m that are coprime to m. And φ(p^k) = (p-1) * p^(k-1) for any prime p, so we get in particular that φ(5^k) = 4 * 5^(k-1), which gives us the (maximum possible value of the) cycle length.