Click here to Skip to main content
12,691,948 members (30,230 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked

Legendre Symbol (C# code)

, 22 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Culculating Legendre Symbol


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;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Junior)
Ukraine Ukraine
No Biography provided

You may also be interested in...


Comments and Discussions

Suggestion[My vote of 2] This has problems... Pin
Matt T Heffron23-Apr-12 13:23
memberMatt T Heffron23-Apr-12 13:23 
QuestionPlease submit as a tip! Pin
Dave Kerr19-Apr-12 23:24
mvpDave Kerr19-Apr-12 23:24 
AnswerRe: Please submit as a tip! Pin
Poisson_PS20-Apr-12 10:19
memberPoisson_PS20-Apr-12 10:19 
QuestionNot an article Pin
Sridhar Patnayak19-Apr-12 21:05
memberSridhar Patnayak19-Apr-12 21:05 
QuestionAgree with others Pin
Mika Wendelius19-Apr-12 11:40
mvpMika Wendelius19-Apr-12 11:40 
QuestionNot enough here. Pin
mark merrens19-Apr-12 10:57
membermark merrens19-Apr-12 10:57 
Suggestionto short for an article Pin
Leonardo Paneque19-Apr-12 10:37
memberLeonardo Paneque19-Apr-12 10: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170117.1 | Last Updated 22 Apr 2012
Article Copyright 2012 by Poisson_PS
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid