Skip to content Skip to sidebar Skip to footer

Find The Next Prime Number In Python

I have a function that takes a number (for example, 5) and returns the first prime number after the input number (in this case, it would be 7). This is my code: def prime(n): n

Solution 1:

You have a few mistakes, most notably np is clearly meant to be the potential primes (it starts at n+1 which is the first potential number that fits your critera "the first prime number after the input number"), and yet you add x to your prime list, which is from range(2,199), you should be using:

isprime.append(j)

Your primality test is also the wrong way round as a result, you should be using:

j%x!=0

Lastly, you can't append a number if that condition is true in one case, it has to be true in all cases (where x is an integer which satisfies 2 <= x < j), because of this you should switch your second set of for loops around (the x loop should be the inner loop), and you should also only loop up to j-1 (the number being tested). Additionally, you should instead choose to not add an item if j % x == 0:

for ...:val_is_prime=Truefor ...:ifj%x==0:val_is_prime=Falsebreakif val_is_prime:isprime.append(j)

This results in the following code:

defprime(n):
    np=[]
    isprime=[]
    for i inrange (n+1,n+200):
        np.append(i)
    for j in np:
        val_is_prime = Truefor x inrange(2,j-1):
            if j % x == 0:
                val_is_prime = Falsebreakif val_is_prime:
            isprime.append(j)
    returnmin(isprime)

And test run:

>>>prime(5)
7
>>>prime(13)
17
>>>prime(23)
29

Note that there's several other efficiency improvements that could be made, but this answer focuses on the mistakes rather than improvements

Solution 2:

Try this one, the most pythonic and clear way to do this that I found (but probably not the most efficient):

defis_prime(x):
    returnall(x % i for i inrange(2, x))

defnext_prime(x):
    returnmin([a for a inrange(x+1, 2*x) if is_prime(a)])

print(next_prime(9))

Solution 3:

https://www.geeksforgeeks.org/python-simpy-nextprime-method/

from sympy import *
  
# calling nextprime function on differnet numbers 
nextprime(7) 
nextprime(13) 
nextprime(2)

Output:

11 17 3

Solution 4:

This code working.

defprime(n):
    next_prime = n + 1
    prime = TruewhileTrue:
        for i inrange(2, next_prime):
            if next_prime%i ==0:
                prime = Falsebreakif prime:
            return next_prime
        else:
            next_prime = next_prime + 1if next_prime % 2 == 0:
                next_prime = next_prime + 1
            prime = Trueif __name__=="__main__":
    print(prime(5))

Solution 5:

Here is one working sample.

inputNumber = int(input("Enter number to find next prime: "))

defnextPrime(inputNum):

    for nextNumToChk inrange(inputNum+1, inputNum +200):
        if nextNumToChk > 1:
            # If num is divisible by any number between 2 and val, it is not primefor i inrange(2, nextNumToChk):
                if (nextNumToChk % i) == 0:
                    breakelse:
                #found the primereturn nextNumToChk

result = nextPrime(inputNumber)
print"Next Prime is : ",result

Output:-

Enter number to find next prime: 5Next Prime is :  7

Post a Comment for "Find The Next Prime Number In Python"