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,329

3 Answers3

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
  • 272,048
  • 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
5

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,310
  • 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
1

Here is some code for answering whether $a$ is a quadratic residue modulo $n$, that correctly handles the case when $\gcd(a, n) > 1$, and also when $a$ or $n$ may be negative (except of course $n$ can't be zero).

def peel(a, p):
    # Returns (k, b) such that a = p^k * b and b is minimal.
    if a == 0: return (1, 0)
    k = 0
    while a % p == 0:
        k += 1
        a = a // p
    return (k, a)

def is_quadratic_residue(a, n):
    for (p, e) in factor(n):
        (k, b) = peel(a % p**e, p)
        if b == 0: continue
        if k % 2: return False
        if p == 2:
            if e == 1: continue
            if b % 4 != 1: return False
            if e >= 3 and b % 8 != 1: return False
        else:  # Euler's criterion
            if power_mod(b, (p - 1)//2, p) != 1:
                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 % p != 1 you can check (x - 1) % p != 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
  • 41,374
  • 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
  • @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
  • @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