In number theory, the Legendre symbol is a multiplicative function with values 1, -1, 0 that is a quadratic character modulo a prime number p: its value on a (nonzero) quadratic residue mod p is 1 and on a quadratic non-residue is -1.
The Legendre symbol was introduced by Adrien-Marie Legendre in 1798 in the course of proving the law of quadratic reciprocity. Its generalizations include the Jacobi symbol and Dirichlet characters of higher order. The notational convenience of the Legendre symbol inspired introduction of several other "symbols" used in algebraic number theory, such as the Hilbert symbol and the Artin symbol.
Using the Code
public int L(int a, int p)
if (a == 1)
if (a % 2 == 0)
return Convert.ToInt32(L(a / 2, p) * Math.Pow(-1, (p * p - 1) / 8));
if ((a % 2 != 0) && (a != 1))
return Convert.ToInt32(L(p % a, a) * Math.Pow(-1, (a - 1) * (p - 1) / 4));