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

Legendre Symbol (C# code)

By , 24 Apr 2012
 

Introduction

This is an alternative to the original Legendre Symbol (C# code)[^]. That implementation suffered from a simple, but significant, error, and significant inefficiency.

Background

See the Wikipedia article "Legendre symbol"[^] for the definition and theory behind this.

Using the code

Here is my implementation of the method:

    public static int Legendre(int a, int p)
    {
      if (p < 2)  // prime test is expensive.
        throw new ArgumentOutOfRangeException("p", "p must not be < 2");
      if (a == 0)
      {
        return 0;
      }
      if (a == 1)
      {
        return 1;
      }
      int result;
      if (a % 2 == 0)
      {
        result = Legendre(a / 2, p);
        if (((p * p - 1) & 8) != 0) // instead of dividing by 8, shift the mask bit
        {
          result = -result;
        }
      }
      else
      {
        result = Legendre(p % a, a);
        if (((a - 1) * (p - 1) & 4) != 0) // instead of dividing by 4, shift the mask bit
        {
          result = -result;
        }
      }
      return result;
    }

Compared to the original this has added:

  • static declaration because it is self-contained (doesn't depend on any class instance values)
  • initial "validation" of p
  • correct checking for a == 0 (Without it, infinite recursion was possible. Try L(3,3).)

Most significantly, the computationally expensive use of Math.Pow() to set the sign of the result has been replaced with all integer operations. This implementation is about 7 times faster than the original tip.

The attached zip file has both implementations, with code for timing and verification of identical results on about 16 million test cases.

Points of Interest

In the original sign manpulation code -1 was being raised to an integer power to be either +1 or -1, and then multiplied by the recursion result to conditionally change the sign of that result. It is only necessary to change the sign if that integer power is odd so a lowest-order bit test will do the job. Since in the original sign manipulation code cases the factors were divided by 8 or 4 (both powers of 2) before being used, the equivalent to the division-then-bit-test is just to shift the bit being tested and avoid the division entirely.

License

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

About the Author

Matt T Heffron
Software Developer (Senior)
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 24 Apr 2012
Article Copyright 2012 by Matt T Heffron
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid