Write a Python function to check if a number is prime.
def is_prime(n):
if n < 2:
return False
for d in range(2, int(n ** 0.5) + 1):
if n % d == 0:
return False
return True
It checks divisibility only up to the square root of n, which is enough because any factor larger than sqrt(n) has a paired factor smaller than sqrt(n).