5

Without strenuous arithmetic.

Is there a program I can download to do so?

What are the quadratic residues modulo $5^4$ or $5^5$?

Thanks!

Vincent
  • 2,337

3 Answers3

6

If you want to know the squares modulo $p^m$ where $p$ is an odd prime, they are congruent to $0$ or to numbers $a p^{2k}$ where $0\le k < m/2$ and the Legendre symbol $\left(\frac ap\right)=1$. For large primes $p$, Legendre symbols can be calculated by regarding them as Jacobi symbols and using the law of quadratic reciprocity for Jacobi symbols.

I don't regard $5$ as a large prime. :-)

Robin Chapman
  • 22,920
  • 1
    And there are many freely available software packages that use arbitrary-precision integers and can efficiently compute Legendre symbols. In linux, Pari would be one. The underlying code is called the GNU Multi-Precision library. As Asaf Karagila points out, there are on-line applications as well. – Ryan Budney Sep 25 '10 at 16:55
  • Pari is also available for Windows and Mac OS X. – Charles Sep 27 '10 at 19:33
6

The Euler criterion is easily generalized to yield the following test for squareness $\!\rm\bmod n$.

Theorem $\ $ Let $\rm\ a,\:n\:$ be integers, with $\rm\:a\:$ coprime to $\rm\:n\ =\ 2^e p_1^{e_1}\cdots p_k^{e_k},\, \ p_i\,$ primes.

$\rm\quad\quad \ x^2\ =\ a\pmod{n}\, $ is solvable for $\rm\:x\:$

$\rm\quad\quad \: \iff\ \ \: a^{(p_i\ -\ 1)/2} \ \ \equiv\ \ 1\ \ (mod\ p_i)\quad\quad\ \ $ for all $\rm\ i\le k$

$\quad\quad\ $ and $\rm\quad\ \ e>1 \:\Rightarrow\: a\equiv 1\ \ (mod\ 2^{2+\delta}\:),\ \ \ \delta = 1\ \ if\ \ e\ge 3\ \ else\ \ \delta = 0$

Proof: See Ireland and Rosen, A Classical Introduction to Modern Number Theory, Proposition 5.1.1 p. 50.

The above criterion is practical if one knows a full factorization of $\rm\:n\:$, since the exponentiations may be quickly computed by repeated squaring.

Beware $\ $ The criterion cannot be expressed equivalently as a simple Jacobi symbol calculation. For example we have $\rm(8|15) = 1\ $ but $8$ is not a square (mod $15$).

Bill Dubuque
  • 282,220
  • Just in case anyone got confused like me, to be clear, if n = 2^1 * 3^1 * 5^0 * 7^1, then p_i = {3, 7}, NOT {3, 5, 7} – Hzz Jun 03 '23 at 17:57
1

For completeness, i.e. to cover the cases where $\gcd(a, n) > 1$ or where $n$ is even, you need to put together the other two answers posted here, so for future reference, here is some code that correctly handles all cases.

def is_quadratic_residue_mod_prime_power(a, p, e):
    """Returns whether a is a quadratic residue modulo p^e,
    where p is prime and e is a nonnegative integer."""
    a = a % (p ** e)
    if a == 0: return True
    # Peel away even powers of p
    while a % (p * p) == 0: a = a // (p * p)
    if a % p == 0: return False
    if p == 2:
        if e == 1: return True
        if a % 4 != 1: return False
        if e >= 3 and a % 8 != 1: return False
        return True
    else: # Euler's criterion
        return power_mod(a, (p - 1)//2, p) == 1

def is_quadratic_residue(a, n): """Returns whether a is a quadratic residue mod n, where a is any integer and n is any nonzero integer.""" assert n != 0 if n < 0: n = -n # Just added for clarity; Sage handles this. # Loop over prime powers p^e in the factorization of n. for (p, e) in factor(n): if not is_quadratic_residue_mod_prime_power(a, p, e): return False return True

As you can see above, it is written in Python, and further relies on two functions (which are available in Sage, but if you're using this elsewhere you'd have to implement yourself):

  • a factor(n) which returns a list of pairs (p, e) such that the product of these $p_i^{e_i}$ is $n$ — for example, factor(126) returns [(2,1), (3,2), (7,1)] because $126 = 2^1 3^2 7^1$.

  • a power_mod(x, n, p) that returns $x^n \bmod p$. This is easy to implement yourself using repeated squaring.

  • it relies on the fact that a % m always returns a positive result when m is positive, even if a is negative. This is how Python does it, but if you're using one of the languages that do it differently, you may have to change some of the definitions (not a big deal; instead of x % m != 1 you can check (x - 1) % m != 0).


A sidenote: you may wonder whether it's possible to answer whether $a$ is a quadratic residue mod $n$, without factoring $n$. If it were possible to do so and also find a square root when $a$ is a quadratic residue, then this would give an algorithm for factoring: pick random $x$ and ask whether $x^2 \bmod n$ is a quadratic residue; if you get a square root $y$ then with probability $1/2$ the fact that $x^2 \equiv y^2 \pmod n$ gives a nontrivial factorization of $n$ by taking $\gcd(n, x-y)$. For only answering yes or no (the quadratic residuosity problem), it is believed to be hard as well (not known whether it is equivalent to factoring, and not known to be easier than factoring).

ShreevatsaR
  • 42,279
  • Note the code is simply a Python implementation of the Theorem in my answer (posted $9$ years prior). – Bill Dubuque Nov 22 '23 at 06:03
  • The remark about factoring $n$ given a nontrivial square root is also $9$ years old, e.g. see here where more general results are proved. – Bill Dubuque Nov 22 '23 at 06:18
  • 1
    @BillDubuque Yes indeed, my main reason for posting an answer with the Python code is that I kept needing it, but every time I looked it up from scratch I'd keep finding answers assuming $\gcd(a, n) = 1$ or that $a, n > 0$ (and not being clear about whether these assumptions mattered). So (for reasons of practical usability) I wanted to do the grunt work carefully and write it down for the general case once, as future reference. – ShreevatsaR Nov 22 '23 at 13:17
  • 2
    @BillDubuque (Also the question asked "Is there a program I can download to do so?" which is probably why I posted it here rather than on some other question.) – ShreevatsaR Nov 22 '23 at 13:49
  • where is the k % 2 condition coming from? – Adi Jan 11 '25 at 11:49
  • 1
    @Adi That's just how the condition goes, e.g. see Robin's answer or Wikipedia. But if you want a proof: suppose $a \equiv x^2 \pmod {p^e}$, and that $a \equiv p^k b \pmod {p^e}$ (where $0 \le k < e$). If $k > 0$ then $p$ divides $a$ so $p$ must divide $x$ as well, and if $x = p^f c$ where $\gcd(c, p) = 1$, then $a \equiv x^2 \equiv p^{2f} c^2 \pmod {p^e}$. So squares always have even powers. – ShreevatsaR Jan 11 '25 at 12:44
  • 1
    @Adi I've rewritten the code to make it easier to read — now instead of writing $a = p^k b$ and checking whether $k$ is odd, I peel away powers of $p^2$ from $a$, and then check whether what remains is divisible by $p$. I've tested this against a naive implementation for all pairs $(a, n)$ each in $[-1000 .. 1000]$ with $n$ nonzero. – ShreevatsaR Jan 11 '25 at 13:29