Use trial division to test whether an integer is prime: reject values below 2, then check whether any integer from 2 through the square root divides it evenly. This beginner-friendly Python function uses math.isqrt for an exact integer bound.
Python program to check if a number is prime
Copy and run this program in Python 3.8 or later:
from math import isqrt
def is_prime(n: int) -> bool:
if n < 2:
return False
for divisor in range(2, isqrt(n) + 1):
if n % divisor == 0:
return False
return True
number = int(input("Enter an integer: "))
if is_prime(number):
print(f"{number} is prime")
else:
print(f"{number} is not prime")
The function returns True for a prime number and False otherwise. The final lines read an integer from the user and print the result.
Why the test only checks through the square root
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. If a number has a factor greater than its square root, it must also have a matching factor smaller than its square root. Checking possible divisors up to that boundary is therefore enough to find any nontrivial factor.
n % divisor == 0 checks whether division leaves no remainder. When it does, the function has found a divisor and returns False. If the loop finishes without finding one, it returns True.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
range excludes its stop value, so isqrt(n) + 1 makes the loop include the integer square root when it is an exact square. Python’s math documentation defines isqrt as returning the integer square root of a nonnegative integer; it avoids using a floating-point square root to set the bound.
How the program handles edge cases
- Negative integers, 0, and 1: the
n < 2check returnsFalse, since none is prime. - 2: the divisor range is empty, so the function returns
True. - A composite number such as 18: the first divisor, 2, divides it evenly, so the function returns
False. - A prime number such as 17: no tested divisor divides it evenly, so the function returns
True.
Python version and input considerations
math.isqrt was added in Python 3.8. For an interpreter older than Python 3.8, use a compatible integer-bound method or update Python before running this version. The official Python documentation page links to the Beginner’s Guide and Tutorial for help getting started.
Rank #2
The sample converts input with int(input(...)). If the user enters text that is not an integer, Python raises ValueError; this short example does not catch that exception.
When this method fits
Trial division through the integer square root is a clear approach for checking one number in a beginner exercise. It is not presented as an optimal primality test for cryptographic-scale integers, and no runtime comparison is established here. If the task is instead to find every prime up to a fixed maximum, that is a different problem and a sieve may be a better fit; no measured crossover point is established.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
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.




