Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Tip/Trick

Legendre Symbol (C# code)

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
22 Apr 2012CPOL 18.2K   1   7
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

C++
//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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestion[My vote of 2] This has problems... Pin
Matt T Heffron23-Apr-12 12:23
professionalMatt T Heffron23-Apr-12 12:23 
QuestionPlease submit as a tip! Pin
Dave Kerr19-Apr-12 22:24
mentorDave Kerr19-Apr-12 22:24 
AnswerRe: Please submit as a tip! Pin
Poisson_PS20-Apr-12 9:19
Poisson_PS20-Apr-12 9:19 
QuestionNot an article Pin
Sridhar Patnayak19-Apr-12 20:05
professionalSridhar Patnayak19-Apr-12 20:05 
QuestionAgree with others Pin
Wendelius19-Apr-12 10:40
mentorWendelius19-Apr-12 10:40 
QuestionNot enough here. Pin
R. Giskard Reventlov19-Apr-12 9:57
R. Giskard Reventlov19-Apr-12 9:57 
Suggestionto short for an article Pin
Leonardo Paneque19-Apr-12 9:37
Leonardo Paneque19-Apr-12 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.