Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.
时间: 2024-05-09 14:21:30 浏览: 100
Sure! Here's the Python code for the is_prime function:
```
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
```
This function takes a single integer argument `n` and returns `True` when `n` is a prime number and `False` otherwise. The function first checks if `n` is less than or equal to 1, as 1 is not a prime number. Then it loops through all the integers from 2 to the square root of `n`, checking if any of them divide `n` without a remainder. If any such integer is found, `n` is not a prime number and the function returns `False`. Otherwise, `n` is a prime number and the function returns `True`.
阅读全文