I have an algorithm for primitive roots for input number $n$ that I believe is $O(n)$ currently. I also have separate algorithms for $\varphi(n)$ and $factorise(n)$ which I believe are both $O(\sqrt{n})$ whose results are used in my primitive root algorithm.

The primitive root alg is based on this Wikipedia entry:

First, compute $\varphi(n)$. Then determine different prime factors of $\varphi(n)$, say $p_1, \cdots, p_k$. Now, for every element m of $\mathbb{Z}_n^*$, compute $$m^{\varphi(n)/p_i}\pmod{n}$$ for $i=1, \cdots,k$, using a fast algorithm for modular exponentiation such as exponentiation by squaring.

A number $m$ for which these $k$ results are all different from $1$ is a primitive root.


I'm looking for examples of efficient algorithms for Primitive roots where time-complexity is $\leq O(\sqrt{n})$.

I have found lots of examples of algorithms that find primitive roots online, however they do not have the time-complexity noted.

  • $\begingroup$ "order operations" ? $\endgroup$ – mercio Feb 11 '18 at 19:07
  • $\begingroup$ I mean time-complexity. Question updated $\endgroup$ – unseen_rider Feb 12 '18 at 19:29

Be careful about what 'n' means in your analysis. Typically in this use it should represent the size of the input, hence this is subexponential since we have to factor.

See Finding a primitive root of a prime number for a nice answer about the process of finding a primitive root.

See Efficient polynomial time algorithms computing industrial-strength primitive roots for a paper on a polynomial time method for probabilistic primitive roots.

Add: See Finding Primitive Roots Pseudo-Deterministically for another discussion. This shows complexities based on factoring (both deterministic and using best known factoring methods). It also points out an answer to the questions below about how far we have to look to find a primitive root: assuming the GRH, $O(\log^6 p)$, which is polynomial in the size of $p$, so the factoring time dominate.

For this purpose, finding phi consists of finding a power which is quite fast. Then factoring it which is the majority of the time spent. The unknown is searching for the first root (which requires one or more modular exponentiations each), and I'm not immediately seeing a hard bound on the number we will have to search. In my experience the factoring dominates the time.

There are some ways to skip some values in practice, but they don't impact the asymptotic number.

Assuming one searches in order, we can look at how the sequence http://oeis.org/A046145 grows. It looks like we find one under $\log^2(n)$ but that's just a few billion examples, and even if that's right we need more analysis to turn that into a proper term.

  • $\begingroup$ For your first point, since you say the alg is subexponential, does this mean that my primitive root alg should be eg $O(\log^2 (n))$ instead of $O(n)$? $\endgroup$ – unseen_rider Feb 14 '18 at 23:46
  • 1
    $\begingroup$ $O(\log^2 n)$ is polynomial in the size of $n$. You're mixing the value of n and the size of n (e.g. number of bits). Almost all the relevant algorithms use the size of n, not the value. A quick test on my code indicated over 90% of the average time around 10^22 was spent factoring, but results will differ based on factoring methods. For complexity of factoring, see en.wikipedia.org/wiki/…. For the final portion, we need to count the cost of each exponentiation as well, but that should still come out polynomial. $\endgroup$ – DanaJ Feb 15 '18 at 3:23
  • $\begingroup$ I presume timing the prim root algorithm for 10 different values of $n$ eg 10000 to 11000 in 100 steps and averaging would give an estimate for time-complexity when $n$ is size 5. I could then repeat this for $n$ of size 6 and 7, and benchmark those to get an idea of its time-complexity? $\endgroup$ – unseen_rider Feb 16 '18 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.