Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Legendre Symbol (C# code)

0.00/5 (No votes)
22 Apr 2012 1  
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;
        } 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here