finding nth prime in python

前端 未结 1 515
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 15:06

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 b

相关标签:
1条回答
  • 2021-01-29 15:24

    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.

    0 讨论(0)
提交回复
热议问题