RosettaCodeData/Task/Lucas-Lehmer-test/Python/lucas-lehmer-test-3.py
2015-11-18 06:14:39 +00:00

17 lines
338 B
Python

import gmpy2 as mp
def lucas_lehmer(n):
if n == 2:
return True
if not mp.is_prime(n):
return False
two = mp.mpz(2)
m = two**n - 1
s = two*two
for i in range(2, n):
sqr = s*s
s = (sqr & m) + (sqr >> n)
if s >= m:
s -= m
s -= two
return mp.is_zero(s)