give me python code to check if a number is prime
- User
give me python code to check if a number is prime
Assistant
Here is a function to check if a number is prime in Python:
pythondef is_prime(num): if num <= 1: return False for i in range(2, int(num**0.5)+1): if num % i == 0: return False return TrueTo use it:
pythonnum = 7 if is_prime(num): print(num, "is prime") else: print(num, "is not prime")This checks all numbers from 2 up to the square root of the input number. If any number evenly divides the input number with no remainder, then the input is not prime. If no such number exists, then the input is prime.
The key aspects are:
- Check numbers from 2 up to the square root of the input
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.