Properties: Let p be an odd prime.
The properties above can be used to build a recursive algorithm to compute the Jacobi symbol
efficiently. In fact, the algorithm is strongly reminiscent of Euclid’s algorithm for the gcd. Here is how the algorithm applies to compute :
- If m > n then use the invariance property: return
.
- If m=0 or m=1, then use(7) : return 0 or 1
- Factor m as 2kl, where l is odd. If k >0 use formulas (7) and (3) : return
.
- Use reciprocity : if m=n=3 mod 4 then return -
; otherwise return
.
As this method is similar to Euclidean GCD algorithm, its complexity too is O (
).
The Jacobi symbol extends the Legendre symbol from primes p to composite odd integers n. One might define the symbol to be +1 if a is a square mod n and -1 if not. However, this would cause the property (3) to fail.
In order to preserve property (3), we define the Jacobi symbol as follows. Let n be an odd positive integer and let a be a nonzero integer with gcd (a, n) =1. Let
n = p1ap2bp3c……prq
be prime factorization of n. Then
The symbols on the right side are Legendre symbols introduced earlier. Note that if n=p, the right side is simply one Legendre symbol, so the Jacobi symbol reduces to the Legendre symbol.