mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
42 lines
976 B
Python
42 lines
976 B
Python
"""
|
||
In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne
|
||
numbers. https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
|
||
|
||
A Mersenne number is a number that is one less than a power of two.
|
||
That is M_p = 2^p - 1
|
||
https://en.wikipedia.org/wiki/Mersenne_prime
|
||
|
||
The Lucas–Lehmer test is the primality test used by the
|
||
Great Internet Mersenne Prime Search (GIMPS) to locate large primes.
|
||
"""
|
||
|
||
|
||
# Primality test 2^p - 1
|
||
# Return true if 2^p - 1 is prime
|
||
def lucas_lehmer_test(p: int) -> bool:
|
||
"""
|
||
>>> lucas_lehmer_test(p=7)
|
||
True
|
||
|
||
>>> lucas_lehmer_test(p=11)
|
||
False
|
||
|
||
# M_11 = 2^11 - 1 = 2047 = 23 * 89
|
||
"""
|
||
|
||
if p < 2:
|
||
raise ValueError("p should not be less than 2!")
|
||
elif p == 2:
|
||
return True
|
||
|
||
s = 4
|
||
m = (1 << p) - 1
|
||
for _ in range(p - 2):
|
||
s = ((s * s) - 2) % m
|
||
return s == 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
print(lucas_lehmer_test(7))
|
||
print(lucas_lehmer_test(11))
|