1

Given integers $p \ge 2$ and $n \ge 1$, I am analyzing the sequence of powers $S = (p^0, p^1, p^2, \dots) \pmod n$.

I know this sequence is always eventually periodic. For example:

If $p=2, n=7$, the sequence becomes (1, 2, 4, 1, 2, 4, ...) which is a pure cycle of length 3.
If $p=2, n=12$, the sequence is (1, 2, 4, 8, 4, 8, ...) which has a pre-period (tail) of (1, 2) and a cycle (4, 8) of length 2.

My question is: Is there a formal method to determine the length of the pre-period and the length of the cycle for any given $p$ and $n$, without having to compute the sequence term by term until a repetition is found?

Kimball
  • 3,197
  • 2
    Possible duplicate of: https://math.stackexchange.com/questions/2000695/order-of-element-in-multiplicative-group-modulo-n-where-n-is-not-prime?rq=1 – wjmccann Jun 12 '25 at 18:22
  • 1
    This is called the "order" of the number (mod $n$), which is the smallest item $k$ such that $p^k\mod n=1$. It can be shown that if $n$ is prime, then the order of every item is $n-1$, however the linked post I provided gives an algorithm if $n$ is not prime – wjmccann Jun 12 '25 at 18:26
  • 1
    The "pre-period" can be computed using the prime factorisations of $p$ and $n$ (it's the maximum of $\lceil k/l\rceil$, where $l$ is the exponent of a prime factor of $p$ and $k$ is the corresponding exponent of the factor in $n$). Regarding the period, if you do have the factorisation, then you can speed it up somewhat by computing the period modulo coprime factors of $n$ (which are also coprime with $n$) and taking their lcm. This way, you might have far fewer elements of the sequence to compute, especially if you have many prime factors. – tomasz Jun 12 '25 at 18:45
  • @wjm, no, it's not true that if $n$ is prime, then everything has order $n-1$. The counterexample $p=2$, $n=7$ is given in the question. What's true is that if $n$ is prime, then everything that isn't a multiple of $n$ has order dividing $n-1$; also, there exist things having order exactly $n-1$. – Gerry Myerson Jun 13 '25 at 05:45
  • Note that if $n$ is prime, and if you can factor $n-1$, then you don't have to compute every power of $p$, you just have to compute $p^d$ for divisors $d$ of $n-1$. – Gerry Myerson Jun 13 '25 at 05:47

1 Answers1

1

If by "formal method", you mean a closed formula, then there is no known formula. In fact, we don't even know if there are infinitely many prime $n$ such that the cycle length of $p=2$ is $n-1$ (a special case of Artin's primitive roots conjecture).

In terms of computational complexity, we can do better than $O(N)$, but not by much. One simple method to beat $O(n)$ is to exploit the birthday paradox, that is we can find a collision in $O(\sqrt{n})$ time with high probability, but this costs a lot of memory. Some fancier method can be found in this post.

The moral of the story is anything related to "order" (of group elements or of groups themselves, or exponents of a group) in group theory is hard to compute in practice or to understand in theory. For example, if we can efficiently compute the order of $(\Bbb Z/n\Bbb Z)^*$ (for $n$ having two prime factors), we can break RSA.

Just a user
  • 22,048