Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

Comments and Discussions

Suggestion[My vote of 2] This has problems... [modified] PinmemberMatt T Heffron23-Apr-12 13:23 
QuestionPlease submit as a tip! PinmvpDave Kerr19-Apr-12 23:24 
AnswerRe: Please submit as a tip! PinmemberPoisson_PS20-Apr-12 10:19 
QuestionNot an article PinmemberSridhar Patnayak19-Apr-12 21:05 
QuestionAgree with others PinmvpMika Wendelius19-Apr-12 11:40 
QuestionNot enough here. Pinmembermark merrens19-Apr-12 10:57 
Suggestionto short for an article PinmemberLeonardo Paneque19-Apr-12 10:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.141216.1 | Last Updated 22 Apr 2012
Article Copyright 2012 by Poisson_PS
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid