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-08 07:18:04 浏览: 14
Sure, here's the code for the is_prime function in Python:
```
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 checks if the input integer is greater than 1 (since 1 is not a prime number), and then iterates through all integers from 2 to the square root of the input integer. If any of these integers divide the input integer evenly, then it's not a prime and the function returns False. Otherwise, the input integer is a prime and the function returns True.
相关问题
: Write a Python function named "is_even" that takes an integer as input and returns True if the number is even, and False otherwise
Sure! Here's the Python code for the `is_even` function that takes an integer as input and returns `True` if the number is even, and `False` otherwise:
```python
def is_even(num):
if num % 2 == 0:
return True
else:
return False
```
You can call this function by passing an integer as an argument like this:
```python
print(is_even(4)) # Output: True
print(is_even(5)) # Output: False
```
In the first call to the function, the input `4` is an even number, so the function returns `True`. In the second call to the function, the input `5` is an odd number, so the function returns `False`.
Write a python function proddigits() that inputs a positive integer number and returns the product of digits of that number.
Here's the Python function `proddigits()` that takes a positive integer as input and returns the product of its digits:
```python
def proddigits(num):
# Initialize product to 1
product = 1
# Convert the integer to a string to loop through its digits
num_str = str(num)
# Loop through each digit in the string
for digit in num_str:
# Multiply the digit to the current product
product *= int(digit)
# Return the final product
return product
```
You can call this function with a positive integer as an argument and it will return the product of its digits. Here's an example usage:
```python
num = 12345
product = proddigits(num)
print(product) # Output: 120
```
In this example, the input integer is 12345 and the product of its digits is 1 x 2 x 3 x 4 x 5 = 120.