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

Tagged as

Go to top

Legendre Symbol (C# code)

, 22 Apr 2012
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Poisson_PS
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 12:23 
QuestionPlease submit as a tip! PinmvpDave Kerr19-Apr-12 22:24 
AnswerRe: Please submit as a tip! PinmemberPoisson_PS20-Apr-12 9:19 
QuestionNot an article PinmemberSridhar Patnayak19-Apr-12 20:05 
QuestionAgree with others PinmvpMika Wendelius19-Apr-12 10:40 
QuestionNot enough here. Pinmembermark merrens19-Apr-12 9:57 
Suggestionto short for an article PinmemberLeonardo Paneque19-Apr-12 9: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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 22 Apr 2012
Article Copyright 2012 by Poisson_PS
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid