Miller-Rabin素性测试(Python实现)
Posted philolif
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Miller-Rabin素性测试(Python实现)相关的知识,希望对你有一定的参考价值。
import random
def fast_power(base, power, n):
result = 1
tmp = base
while power > 0:
if power&1 == 1:
result = (result * tmp) % n
tmp = (tmp * tmp) % n
power = power>>1
return result
def Miller_Rabin(n, iter_num):
# 2 is prime
if n == 2:
return True
# if n is even or less than 2, then n is not a prime
if n&1 == 0 or n<2:
return False
# n-1 = (2^s)m
m,s = n - 1,0
while m&1==0:
m = m>>1
s += 1
# M-R test
for _ in range(iter_num):
b = fast_power(random.randint(2,n-1), m, n)
if b==1 or b== n-1:
continue
for __ in range(s-1):
b = fast_power(b, 2, n)
if b == n-1:
break
else:
return False
return True
if __name__ == "__main__":
# example
print(Miller_Rabin(49139, 10))
print(Miller_Rabin(561, 10))
以上是关于Miller-Rabin素性测试(Python实现)的主要内容,如果未能解决你的问题,请参考以下文章