Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To check whether one integer is prime, test possible divisors up to its integer square root. To generate every prime below a limit, use a sieve such as SymPy’s primerange. These are different tasks: trial division is a clear standard-library solution for one candidate, while a sieve is designed to produce a bounded list.

What counts as a prime number?

A prime is an integer greater than 1 whose only positive divisors are 1 and itself. That definition makes the boundary cases important: negative numbers, 0, and 1 are not prime. The number 2 is prime; it is also the only even prime.

Check whether one number is prime with Python

For a candidate n, it is enough to test possible divisors up to floor(sqrt(n)). If n had a factor larger than its square root, the paired factor would be smaller. Python’s standard-library math.isqrt gives the exact integer square root without floating-point rounding.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from math import isqrt

def is_prime(n: int) -> bool:
    if n < 2:
        return False
    if n % 2 == 0:
        return n == 2
    for divisor in range(3, isqrt(n) + 1, 2):
        if n % divisor == 0:
            return False
    return True

The function first rejects values below 2, then handles even numbers directly. For an odd candidate, it checks odd divisors starting at 3 and stops at the integer square root. Skipping even divisors avoids checks that cannot succeed after the even case has been handled.

For example, is_prime(-1), is_prime(0), and is_prime(1) return False; is_prime(2) returns True; and is_prime(12) returns False because it is even and not 2. The odd prime 13 returns True. The odd composite 45 returns False because 3 divides it; the search does not need to continue beyond its square-root bound.

This is a straightforward approach for learning and checking one moderate-sized integer. It is not a benchmarked performance claim, and trial division is not the best fit for very large candidates.

Python documents math.isqrt as the integer square-root function.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generate all primes below a limit

If you need a list of primes rather than a yes-or-no answer for one number, use a sieve-based method. SymPy’s primerange(a, b) returns primes in the interval that includes a and excludes b.

from sympy import primerange

primes = list(primerange(1, 30))
print(primes)

The result is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]. The upper endpoint is excluded, so this call returns primes less than 30.

SymPy also documents a dynamically growing Sieve of Eratosthenes. Sieve-based generation is generally faster for producing a range of primes than testing each candidate independently, but it uses more memory because values are stored. Choose it when you need a bounded list and can use the additional dependency and memory.

Choose the approach for your task

Approach Best fit Tradeoff or caveat
Trial division with math.isqrt Learning or testing one moderate-sized integer Simple and uses the standard library, but does not scale like specialized primality routines for very large inputs.
SymPy primerange or sieve Generating primes in a bounded interval Generally faster for range generation, but uses more memory because values are stored; requires SymPy.
SymPy isprime Convenient library-based checks of individual values Requires SymPy. For inputs greater than 264, its documented method is a probable-prime test rather than a proof.

For a small, self-contained program, the standard-library function is often the simplest choice. If you already use SymPy or need to generate many primes, its number-theory tools avoid implementing the sieve yourself. There is no universal numeric cutoff established here; the right choice depends on whether you are testing one candidate or generating a range, whether you can add a dependency, and how much memory you can use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use SymPy for a library-based primality check

SymPy provides isprime for checking one value. Its documentation describes deterministic Miller–Rabin tests for smaller inputs and a strong BPSW test for inputs greater than 264. SymPy describes the latter as a probable-prime test and reports no known counterexamples; do not treat that documented result for larger inputs as a mathematical proof.

See SymPy’s isprime, primerange, and sieve documentation for the library APIs and their behavior.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.