1
$\begingroup$

By Euler Criterion, it is true that if $p \equiv 1 \pmod{4}$ then there exists a integer $x$ such that $x^2 \equiv -1 \pmod{p}$.

But how do I find $x$?

2 Answers 2

3

Probably the easiest algorithmic approach is to try to find a primitive root of $p$, although actually a primitive root as such is a stronger requirement than is needed. Note that I'm assuming $p$ is prime.

Choose candidate numbers $a_i$ - generally it is better if $a_i$ and $p{-}a_i$ are non-squares. Then use modular exponentiation to find $a_i^s \bmod p$ where $s$ is the largest odd factor of $p{-}1$ - that is, $p-1=s\cdot 2^d$. If this is equivalent to $1$ or $-1(\equiv p-1)$, you need to pick another candidate. Otherwise you can square this until you get $-1$. Then the number at the previous step is a square root of $-1$.

This works because $\phi(p-1)$ of the numbers less than $p$ will be primitive roots, so they should be relatively easy to find, and the looser constraint here mean that even more number may be able to provide a suitable path to finding your required $x$.

  • 1
    In fact, if $a$ is not a quadratic residue modulo $p$, then $a^{(p-1)/4}$ does it, since $$a^{\frac{p-1}{2}} \equiv \biggl(\frac{a}{p}\biggr) \pmod{p}.$$ So if one picks a quadratic nonresidue, one never has $a_i^s \equiv -1 \pmod{p}$. So one just needs to find a quadratic nonresidue. The smallest such is always a prime, thus one can check the primes until a nonresidue is found, typically soon.2017-01-11
2

Suppose $p \equiv 1 \mod{4}$ with $p$ being prime. Then there is an integer $n$ such that $p = 4n + 1$. Let $a = (2n)!$. We have \begin{align} a^2 &\equiv (\prod_{j = 1}^{2n} j)(\prod_{j = 1}^{2n} -j) \mod{p}\\ &\equiv \prod_{j = 1}^{2n} j(-j) \mod{p}\\ &\equiv \prod_{j = 1}^{2n}j(p - j) \mod{p}\\ &\equiv \prod_{j = 1}^{2n}j(4n + 1 - j) \mod{p}\\ &\equiv (\prod_{j = 1}^{2n} j)( \prod_{j = 2n + 1}^{4n} j) \mod{p}\\ &\equiv (4n)! \mod{p}\\ &\equiv (p - 1)! \mod{p} \end{align} and by Wilson's Theorem $(p - 1)! \equiv - 1 \mod{p}$ so we have $a^2 \equiv - 1 \mod{p}$. Thus $a$ is such an integer.

  • 0
    Thanks a lot! While this gives the solution, is it possible to find a solution which is slightly easier to compute by programming?2017-01-11
  • 0
    @BillGates If your using a program to find such an integer given a specific $p$, then you can just compute the quadratic residues modulo $p$, i.e compute $x^2 \mod p$ for $1 \leq x \leq p - 1$. In fact you can speed this method up with tricks such as starting at $\lfloor \sqrt{p - 1} \rfloor$ instead of $1$.2017-01-11
  • 0
    Yes. But the problem is that I need to compute quadratic residues of all primes upto a certain range. It won't be feasible to iterate $p$ times for each prime.2017-01-11