galois.legendre_symbol(a: int, p: int) int

Computes the Legendre symbol (ap).

Parameters:
a: int

An integer.

p: int

An odd prime p3.

Returns:

The Legendre symbol (ap) with value in {0,1,1}.

Notes

The Legendre symbol is useful for determining if a is a quadratic residue modulo p, namely aQp. A quadratic residue a modulo p satisfies x2a (mod p) for some x.

(ap)={0,p | a1,aQp1,aQp

References

Examples

The quadratic residues modulo 7 are Q7={1,2,4}. The quadratic non-residues modulo 7 are Q7={3,5,6}.

In [1]: [pow(x, 2, 7) for x in range(7)]
Out[1]: [0, 1, 4, 2, 2, 4, 1]

In [2]: for a in range(7):
   ...:     print(f"({a} / 7) = {galois.legendre_symbol(a, 7)}")
   ...: 
(0 / 7) = 0
(1 / 7) = 1
(2 / 7) = 1
(3 / 7) = -1
(4 / 7) = 1
(5 / 7) = -1
(6 / 7) = -1