isPrime#

Check if a number is prime.

Introduction#

The isPrime function checks if a number is prime using the Baillie-PSW and Miller-Rabin primality tests, using the function mpz_probab_prime_p implemented in the C library gmp.h (refer this for more details). The function can check for primality of a number of any size if the force parameter is set to True. However, for performance reasons, the function is limited to checking primality of numbers <= 2^11 if force=False.

isPrime(N, k=10, force=False)#
Parameters:
  • N (int) – The number to check for primality, is asserted to be <= 2^11 if force=False for performance reasons.

  • k (int) – The number of iterations for the Miller-Rabin primality test, defaults to 10.

  • force (bool) – If True, the function will check for primality of a number of any size, defaults to False for performance reasons.

Returns:

True or False, based on whether the number is prime is not.

Return type:

bool

Example Usage#

# Example usage of isPrime to check if a number is prime
from cryptosystems import isPrime
print(isPrime(19)) # True
print(isPrime(20)) # False