Given a positive integer 'n', the task is to find a prime number greater than or equal to 'n'. the output should be y
时间: 2023-12-03 20:44:24 浏览: 98
Sure, I can help you with that. Here's a Python code snippet that finds the next prime number greater than or equal to a given integer 'n':
```python
def is_prime(num):
"""
Returns True if the given number is prime, False otherwise.
"""
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def next_prime(n):
"""
Returns the next prime number greater than or equal to the given number.
"""
num = n
while True:
if is_prime(num):
return num
num += 1
# Example usage
n = 10
print(next_prime(n)) # Output: 11
```
The `is_prime()` function checks if a given number is prime or not by iterating from 2 to the square root of the number and checking if it is divisible by any of the numbers in between.
The `next_prime()` function takes an integer `n` as input and starts iterating from `n` until it finds the next prime number using the `is_prime()` function. Once it finds a prime number, it returns it as output.
Hope this helps!
阅读全文