Published on

One-liner to check if a number is prime

133 words1 min read–––

This lambda function uses a list comprehension to check if the number x is divisible by any number from 2 to the square root of x, plus 1. If x is not divisible by any of these numbers, it returns True, indicating that x is prime.

is_prime = lambda x: all(x % i for i in range(2, int(x ** 0.5) + 1))