Find the next prime number using the logic in the prewritten code?

Still relatively new to Python and I currently have one function that identifies prime numbers as a bool of true or false. If the input value is prime then I will have a second function identify the next prime number after it. Here is what I have so far:

def is_prime(n):
>if n in range(0, 2):
>>n = False

>for i in range(2, n):
>>if n % i == 0:
>>>n = False

>>else:
>>>n = True 

>return n


def find_next_prime(n):

*Use the first function is_prime to run this second function*

Upvotes: 0

Views: 74

Answers (2)

Mayank
Mayank

Reputation: 158

def is_prime(n):
    if n in range(0, 2):
        return True

    for i in range(2, n):
        if n % i == 0:
            return False
    return True


def find_next_prime(n):
    if is_prime(n):
        print("{} is prime".format(n))
        n += 1
        while not is_prime(n):
            n += 1
        print("{} is next prime".format(n))
    else:
        print("{} is not prime".format(n))

You can then run it

find_next_prime(4)  \\ output --> 4 is not prime
find_next_prime(53) \\ output --> 53 is prime \n 59 is next prime

Upvotes: 1

AmirHmZ
AmirHmZ

Reputation: 556

Try this :

def find_next_prime(n):
    n += 1
    while not is_prime(n):
        n += 1
    return n

Upvotes: 1

Related Questions