Skip to content

Instantly share code, notes, and snippets.

@0xdeafbeef
Created March 14, 2019 21:21
Show Gist options
  • Save 0xdeafbeef/e11518bdb31995cbcc4c95d33a2ce720 to your computer and use it in GitHub Desktop.
Save 0xdeafbeef/e11518bdb31995cbcc4c95d33a2ce720 to your computer and use it in GitHub Desktop.
def jacobi(a, n):
assert(n > a > 0 and n%2 == 1)
t = 1
while a != 0:
while a % 2 == 0:
a /= 2
r = n % 8
if r == 3 or r == 5:
t = -t
a, n = n, a
if a % 4 == n % 4 == 3:
t = -t
a %= n
if n == 1:
return t
else:
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment