def baillie_psw(candidate): """Perform the Baillie-PSW probabilistic primality test on candidate""" # Check divisibility by a short list of primes less than 50 for known_prime in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]: if candidate == known_prime: return True elif candidate % known_prime == 0: return False # Now perform the Miller-Rabin primality test base 2 if not miller_rabin_base_2(candidate): return False # Check that the number isn't a square number, as this will throw out # calculating the correct value of D later on (and means we have a # composite number) # the slight ugliness is from having to deal with floating point numbers if int(sqrt(candidate) + 0.5) ** 2 == candidate: return False # Finally perform the Lucas primality test D = D_chooser(candidate) if not lucas_pp(candidate, D, 1, (1-D)/4): return False # You've probably got a prime! return True
def baillie_psw(candidate): """Perform the Baillie-PSW probabilistic primality test on candidate""" # Check divisibility by a short list of primes less than 50 for known_prime in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]: if candidate == known_prime: return True elif candidate % known_prime == 0: return False # Now perform the Miller-Rabin primality test base 2 if not miller_rabin_base_2(candidate): return False # These two composite numbers are Miller-Rabin pseudoprimes and cause # an infinite loop in the below implementation of the Lucas Test if candidate in set([1194649, 12327121]): return False # Finally perform the Lucas primality test D = D_chooser(candidate) if not lucas_pp(candidate, D, 1, (1-D)/4): return False # You've probably got a prime! return True
def baillie_psw(candidate): """Perform the Baillie-PSW probabilistic primality test on candidate""" # Check divisibility by a short list of primes less than 50 for known_prime in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 33, 37, 41, 43, 47]: if candidate == known_prime: return True elif candidate % known_prime == 0: return False # Now perform the Miller-Rabin primality test base 2 if not miller_rabin_base_2(candidate): return False # Finally perform the Lucas primality test D = D_chooser(candidate) if not lucas_pp(candidate, D, 1, (1-D)/4): return False # You've probably got a prime! return True
def baillie_psw(candidate): """Perform the Baillie-PSW probabilistic primality test on candidate""" for known_prime in [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 ]: if candidate == known_prime: return True elif candidate % known_prime == 0: return False if not miller_rabin_base_2(candidate): return False if int(candidate**0.5 + 0.5)**2 == candidate: return False D = D_chooser(candidate) if not lucas_pp(candidate, D, 1, (1 - D) // 4): return False return True
def testMillerRabinOnEvens(): for x in range(30, 10000000, 2): assert not miller_rabin_base_2(x)
def testMillerRabinFailsForStrongPseudoprimes(): for x in [2047, 3277, 4033, 4681, 8321, 15841, 29341, 42799, 49141]: assert miller_rabin_base_2(x)
def testMillerRabinFailsForNonPrimes(): for x in [ 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30 ]: assert not miller_rabin_base_2(x)
def testMillerRabinPassesForLargerPrimes(): """Largest primes for which the base-2 MR test definitely pass""" for x in [2003, 2011, 2017, 2029, 2039]: assert miller_rabin_base_2(x)
def testMillerRabinPassesForSmallPrimes(): for x in [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]: assert miller_rabin_base_2(x)
def testMillerRabinOnEvens(): for x in range(30, 10000000, 2): assert not miller_rabin_base_2(x)
def testMillerRabinFailsForStrongPseudoprimes(): for x in [2047, 3277, 4033, 4681, 8321, 15841, 29341, 42799, 49141]: assert miller_rabin_base_2(x)
def testMillerRabinFailsForNonPrimes(): for x in [4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30]: assert not miller_rabin_base_2(x)
def testMillerRabinPassesForLargerPrimes(): """These are the largest primes for which the base-2 MR test will definitely pass""" for x in [2003, 2011, 2017, 2029, 2039]: assert miller_rabin_base_2(x)
def testMillerRabinPassesForSmallPrimes(): for x in [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]: assert miller_rabin_base_2(x)