Skip to content

Instantly share code, notes, and snippets.

@Storyyeller
Created December 16, 2018 01:05
Show Gist options
  • Save Storyyeller/d95f99c8ed46c2c3b8c74d847a7c2790 to your computer and use it in GitHub Desktop.
Save Storyyeller/d95f99c8ed46c2c3b8c74d847a7c2790 to your computer and use it in GitHub Desktop.
Implementation of the Jacobi symbol
_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