Skip to content

Instantly share code, notes, and snippets.

@wataken44
Last active June 18, 2019 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wataken44/13a8960c6f00e20e2ab743415617e478 to your computer and use it in GitHub Desktop.
Save wataken44/13a8960c6f00e20e2ab743415617e478 to your computer and use it in GitHub Desktop.
JAESCHKE_PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
def is_prime_miller_rabin(n):
if n in JAESCHKE_PRIMES:
return True
if (n == 1) or (n & 1 == 0):
return False
d = n - 1
while (d & 1 == 0):
d = d >> 1
for a in JAESCHKE_PRIMES:
if a > n:
continue
t = d
y = pow(a, t, n)
while (t != n - 1) and (y != 1) and (y != n - 1):
y = (y * y) % n
t = t << 1
if (y != n - 1) and (t & 1 == 0):
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment