TITLE: How to find the solutions for the n-th root of unity in modular arithmetic?
QUESTION [16 upvotes]: $$\begin{align*}
x^n\equiv1&\pmod p\quad(1)\\
x^n\equiv-1&\pmod p\quad(2)\end{align*}$$
Where $n\in\mathbb{N}$,$\quad p\in\text{Primes}$ and $x\in \{0,1,2\dots,p-1\}$.
How we can find the solutions for x without calculate all numbers (from 0 to p-1) high power n?
Or, in other words, how to find the n-th root of unity (and the negative of unity) using modular arithmetic without do all calculus table?

REPLY [12 votes]: Let $p$ be an odd prime. For the congruence $x^n \equiv 1 \pmod{p}$, let $g$ be a primitive root of $p$. There are very good probabilistic algorithms for finding a primitive root of $p$. 
After that, everything is easy.  
Let $d=\gcd(n,p-1)$. Then our congruence has $d$ incongruent solutions modulo $p$. One solution is $h$, where $h \equiv g^{(p-1)/d}\pmod{p}$. To get them all, we look at $h^t$, where $t$ ranges from $0$ to $d-1$.
For the congruence $x^n \equiv -1\pmod{p}$, we must first determine whether there is a solution. There is a solution if and only if $(p-1)/d$ is even. One solution is then $g^{(p-1)/2d}$. The other solutions can be obtained by multiplying by $n$-th roots of $1$.