I need a function that checks whether a number is prime or not. Write this in Python.
- User
I need a function that checks whether a number is prime or not. Write this in Python.
Assistant
Here's a simple implementation of a prime checker function in Python:
def is_prime(number: int) -> bool: if number < 2: return False for i in range(2, int(number ** 0.5) + 1): if number % i == 0: return False return True # Test the function print(is_prime(2)) # True print(is_prime(3)) # True print(is_prime(4)) # False print(is_prime(15)) # False print(is_prime(17)) # True print(is_prime(18)) # False print(is_prime(23)) # TrueNote that this function uses a simple optimization, where it checks only up to the square root of the number. If the number is composite, one of its factors will always be less than or equal to its square root. If you don't find any factor by checking up to the square root, the number is prime. This optimization drastically reduces the number of checks for larger numbers.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.