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 lucas_lehmer_test p 7 true lucas_lehmer_test p 11 false m_11 2 11 1 2047 23 89
def lucas_lehmer_test(p: int) -> bool:
    

    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))