Skip to content Skip to sidebar Skip to footer

Finding Nth Prime In Python

I wrote the following segment of code in Python to find the nth number. I don't understand why it doesn't work. Can you please only give me a hint or point out exactly which bit is

Solution 1:

Your dont print anything. Your function works.

term = int(input("What prime do you want to find?   "))
prime_list=[2]

def prime_search(term):
    x=3
    while len(prime_list) <= term:
        if all(x % y != 0 for y in range(2,x)):
            prime_list.append(x)
        x += 1
    return prime_list[term-1]

print(prime_search(term))

Output:

What prime do you want to find?   5
11

However i advise you to look up prime sieve if you really want to use this.


Post a Comment for "Finding Nth Prime In Python"