Created
December 16, 2018 01:05
-
-
Save Storyyeller/d95f99c8ed46c2c3b8c74d847a7c2790 to your computer and use it in GitHub Desktop.
Implementation of the Jacobi symbol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_s2 = {1:1, 3:-1, 5:-1, 7:1} | |
def jacobi(a,n): | |
a = a % n | |
assert(a) | |
res = 1 | |
while not a&1: | |
res *= _s2[n % 8] | |
a = a//2 | |
if a == 1: | |
return res | |
if n%4 == 3 and a%4 == 3: | |
res = -res | |
return res * jacobi(n, a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment