Skip to content

LegendreSymbol ​

The Legendre symbol (n/p): whether n is a quadratic residue mod the odd prime p.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaLegendre symbolMathWorldLegendreSymbolWikidataQ748339Wolfram LanguageJacobiSymbol!
LegendreSymbol(n, p)the Legendre symbol (np)\left(\frac{n}{p}\right).
SageMathlegendre_symbol(n, p)SymPylegendre_symbol(n, p)
Typed signatures
TypePackageApplies
(integer, integer) -> integercompute-enginealways

Domain: Number theory

Details
  • 1 if nn is a nonzero quadratic residue mod pp, −1-1 if it is a nonresidue, 0 if p∣np\mid n.
  • Defined only for an odd prime pp; compute-engine leaves the call symbolic for any other pp.
  • The prime case of JacobiSymbol and KroneckerSymbol — all three agree wherever the domains overlap.

Plot

Examples

Basic #

2 is a quadratic residue mod 7: 3² ≡ 2 (mod 7)

live ↗
InLegendreSymbol(2,7)\mathrm{LegendreSymbol}(2, 7)Out11

3 is not a quadratic residue mod 7

live ↗
InLegendreSymbol(3,7)\mathrm{LegendreSymbol}(3, 7)Out−1-1

0 when p divides n

live ↗
InLegendreSymbol(14,7)\mathrm{LegendreSymbol}(14, 7)Out00
Scope #

Listable in nn: the residues mod 7 are 1, 2, 4

live ↗
InLegendreSymbol([1,2,3,4,5,6],7)\mathrm{LegendreSymbol}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack, 7)Out[1,1,−1,1,−1,−1]\bigl\lbrack1, 1, -1, 1, -1, -1\bigr\rbrack
Properties #

−1-1 is a residue mod pp exactly when p≡1(mod4)p\equiv1\pmod4

live ↗
InLegendreSymbol(−1,13)\mathrm{LegendreSymbol}(-1, 13)Out11

Euler's criterion: (a/p)≡a(p−1)/2(modp)(a/p)\equiv a^{(p-1)/2}\pmod p, here 33≡6≡−13^3\equiv6\equiv-1. See PowerMod

live ↗
InLegendreSymbol(3,7)=PowerMod(3,3,7)−7\mathrm{LegendreSymbol}(3, 7)=\mathrm{PowerMod}(3, 3, 7)-7OutTrue\mathrm{True}
Possible issues #

9 is not prime, so the call stays symbolic; see JacobiSymbol

live ↗
InLegendreSymbol(2,9)\mathrm{LegendreSymbol}(2, 9)OutLegendreSymbol(2,9)\mathrm{LegendreSymbol}(2, 9)
About
Examples7
Checked againstWolfram Language 6 agree1 differSageMath 6 agree1 errorSymPy 6 agree1 errormathlib4 4 agree1 unevaluated1 error
CrosswalkWikipediaMathWorldWikidataWolfram LanguageSageMathSymPy

Implementation

mappedsympypackages/oracle/src/mappings.ts

SymPy's legendre_symbol raises unless the lower argument is an odd prime.

mappedsagepackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts

Mathlib's legendreSym needs a Fact p.Prime instance a literal cannot supply, so this reads jacobiSym a p, which Mathlib proves equal to legendreSym p a at an odd prime p (jacobiSym.legendreSym.to_jacobiSym); a composite or even p is outside the Legendre domain and gives the Jacobi value or zero.

See also: JacobiSymbol, KroneckerSymbol