65.9K
CodeProject is changing. Read more.
Home

Legendre Symbol (C# code)

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (2 votes)

Apr 23, 2012

CPOL
viewsIcon

19292

Culculating Legendre Symbol

Introduction

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[1] 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

//Calculating Legandre symbol
        public int L(int a, int p)
        {
            if (a == 1)
            {
                return 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));
            }
            return 0;
        }