In mathematics, the LucasLehmer test LLT is a primality test for Mersenne numbers.  https:en.wikipedia.orgwikiLucasE28093Lehmerprimalitytest A Mersenne number is a number that is one less than a power of two. That is Mp  2p  1 https:en.wikipedia.orgwikiMersenneprime The LucasLehmer test is the primality test used by the Great Internet Mersenne Prime Search GIMPS to locate large primes. Primality test 2p  1 Return true if 2p  1 is prime  lucaslehmertestp7 True  lucaslehmertestp11 False M11  211  1  2047  23  89
# 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))
