Last active
June 18, 2019 11:14
-
-
Save wataken44/13a8960c6f00e20e2ab743415617e478 to your computer and use it in GitHub Desktop.
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
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