%mathpiper,def="JacobiSymbol"

/* Implementation of some number theoretical functions for MathPiper */
/* (C) 2002 Pablo De Napoli <pdenapo@yahoo.com> under GNU GPL */

/** Compute the Jacobi symbol JS(m/n) - n must be odd, both positive.
See the Algo book for documentation.

*/

10 # JacobiSymbol(_a, 1) <-- 1;
15 # JacobiSymbol(0, _b) <-- 0;
18 # JacobiSymbol(_a, _b) _ (Gcd(a,b)>1) <-- 0;

20 # JacobiSymbol(_a, b_IsOdd)_(a>=Abs(b) Or a<0) <-- JacobiSymbol(Modulo(a,Abs(b)),Abs(b));

30 # JacobiSymbol(a_IsEven, b_IsOdd) <--
[
	Local(c, s);
	// compute c,s where a=c*2^s and c is odd
	{c,s}:=FindPrimeFactorSimple(a, 2);	// use the "Simple" function because we don't expect a worst case here
	If(Modulo(s,2)=1 And Abs(Modulo(b,8)-4)=1, -1, 1) * JacobiSymbol(c,b);
];

40 # JacobiSymbol(a_IsOdd, b_IsOdd) <-- If(Modulo(a,4)=3 And Modulo(b,4)=3, -1, 1) * JacobiSymbol(b,a);

%/mathpiper



%mathpiper_docs,name="JacobiSymbol",categories="User Functions;Number Theory"
*CMD JacobiSymbol --- functions related to finite groups
*STD
*CALL
	JacobiSymbol(m,n)

*PARMS
{m}, {n} -- integers, $n$ must be odd and positive

*DESC

The Jacobi symbol $[m/n;]$ is defined as the product of the Legendre symbols of the prime factors $f[i]$ of $n=f[1]^p[1]*...*f[s]^p[s]$,
$$ [m/n;] := [m/f[1];]^p[1]*...*[m/f[s];]^p[s] $$.
(Here we used the same notation $[a/b;]$ for the Legendre and the Jacobi symbols; this is confusing but seems to be the current practice.)
The Jacobi symbol is equal to $0$ if $m$, $n$ are not mutually prime (have a common factor).
The Jacobi symbol and the Legendre symbol have values $+1$, $-1$ or $0$.
If $n$ is prime, then the Jacobi symbol is the same as the Legendre symbol.

The Jacobi symbol can be efficiently computed without knowing the full factorization of the number $n$.

*E.G.

In> JacobiSymbol(7,15)
Result: -1;

*SEE Gcd, LegendreSymbol, IsQuadraticResidue
%/mathpiper_docs