LEGENDRE
This function evaluates the Legendre symbol (a/p) for an integer a and an odd prime p. It classifies whether a is a quadratic residue modulo p.
The value is defined by:
\left(\frac{a}{p}\right)= \begin{cases} 0, & p \mid a \\ 1, & a \text{ is a quadratic residue mod } p \\ -1, & a \text{ is a quadratic nonresidue mod } p \end{cases}
This test is fundamental in modular arithmetic and finite field computations.
Excel Usage
=LEGENDRE(a, p)
a(int, required): Integer whose residue class is tested.p(int, required): Odd prime modulus.
Returns (int): Legendre symbol value -1, 0, or 1.
Example 1: Legendre symbol zero case
Inputs:
| a | p |
|---|---|
| 0 | 7 |
Excel formula:
=LEGENDRE(0, 7)
Expected output:
0
Example 2: Legendre symbol residue case
Inputs:
| a | p |
|---|---|
| 2 | 7 |
Excel formula:
=LEGENDRE(2, 7)
Expected output:
1
Example 3: Legendre symbol nonresidue case
Inputs:
| a | p |
|---|---|
| 3 | 7 |
Excel formula:
=LEGENDRE(3, 7)
Expected output:
-1
Example 4: Equivalent residue classes produce same value
Inputs:
| a | p |
|---|---|
| 10 | 7 |
Excel formula:
=LEGENDRE(10, 7)
Expected output:
-1
Python Code
from sympy import legendre_symbol as sympy_legendre_symbol
def legendre(a, p):
"""
Compute the Legendre symbol modulo an odd prime.
See: https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.residue_ntheory.legendre_symbol
This example function is provided as-is without any representation of accuracy.
Args:
a (int): Integer whose residue class is tested.
p (int): Odd prime modulus.
Returns:
int: Legendre symbol value -1, 0, or 1.
"""
try:
return int(sympy_legendre_symbol(a, p))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Integer whose residue class is tested.
Odd prime modulus.