用miller-rabin算法判断137是否素数
时间: 2024-06-13 18:06:00 浏览: 87
以下是使用Miller-Rabin算法判断137是否素数的Python代码:
```python
import random
def power(x, y, p):
res = 1
x = x % p
while y > 0:
if y & 1:
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def miller_rabin(n, k):
if n == 2 or n == 3:
return True
if n <= 1 or n % 2 == 0:
return False
# Write n-1 as 2^r*d
d = n - 1
r = 0
while d % 2 == 0:
d //= 2
r += 1
# Witness loop
for _ in range(k):
a = random.randint(2, n - 2)
x = power(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = power(x, 2, n)
if x == n - 1:
break
else:
return False
return True
if miller_rabin(137, 10):
print("137是素数")
else:
print("137不是素数")
```
输出结果为:137是素数。
阅读全文