Python script to find nth prime number

2024/10/5 18:49:25

I'm new to Python and I thought I'd try to learn the ropes a bit by writing a function to find the nth prime number, however I can't get my code to work properly. No doubt this is due to me missing something fundamental, but I'd appreciate your help in finding where it went wrong!

c=2
n=input("Which prime would you like? ")
n=int(n)
a=[]
l=len(a)while l<=n:if c==2:a.append(c)elif (c % 2 ==0): #c is evenbreakelif (c % 2 !=0): #c is oddif c<7:a.append(c)elif c >=7:for i in range(3,int((c+1)/2)):if (c % i ==0):breakelse:a.append(c)else:            c+=1
a[n]

Thanks! Andrew

Answer

This can be a start. This checks whether the number N is divisible by all numbers from 2 to int(sqrt(N)) + 1, where the int function truncates the square root of N. The all() function in python returns True if all members of a list satisfy some condition (here not zero). You should set an upper bound as this is not very efficient for really large n. I'll leave that to you.

def nthprime(n):import mathstart = 2count = 0while True:if all([start % i for i in range(2, int(math.sqrt(start)) + 1)]) != 0:count += 1if count == n:return startstart += 1 In [91]: nthprime(50)
Out[91]: 229In [92]: nthprime(100)
Out[92]: 541

Tested with this.

https://en.xdnf.cn/q/120116.html

Related Q&A

Printing values from list within an input range

I have an unordered list, lets say:lst = [12,23,35,54,43,29,65]and the program will prompt the user to input two numbers, where these two numbers will represent the range.input1 = 22input2 = 55therefor…

An issue with the tag add command of the ttk.Treeview widget - cant handle white space

I have noticed an issue with using the tag add command of a ttk.Treeview widget when activated with the tk.call() method. That is, it cant handle white space in the value of the str() elements of its i…

How to show the ten most overdue numbers in a list

I have asked a question before about this bit of code and it was answered adequately, but I have an additional question about showing the ten most overdue numbers. (This program was a part of an in-cla…

Connect a Flask webservice from a device which is not on the same network

I am not an expert in web programming and know very little about it. I am trying to run a webservice on an EC2 instance (Windows Server 2012R2) and the webservice is written in Python using Flask packa…

why int object is not iterable while str is into python [duplicate]

This question already has answers here:Why is int" not iterable in Python, but str are?(4 answers)Closed 2 years ago.As i know we can not iterate int value while we can iterate strings in python.…

an irregular anomaly in python tuple

i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same code in pthon run throug…

AttributeError: type object Employee has no attribute Worker

Taking a class on Python coding and trying to use inheritance to code an answer to this problem: Write an Employee class that keeps data attributes for the following piece of information: Employee name…

How to add tag for numbers which in brackets using python regex?

The default strings is:strings123[abc123def456]strings456Add tag for number:strings[abc<span>123</span>def<span>456</span>]strings

How to write program run matrix as below in python?

Thanks for everyones reply. I will explain here. Suppose there is a given matrixx y B = [5,-4,5,-6] [[0,0,0,0], [[0,1,0,1],[0,0,0,0], [0,0,0,0],[0,0,0,0],…

Scraping Dynamic Information

I recently started with coding, I use Python and Pycharm. I Installed and imported the needed "Add-ons" like Selenium. For my first project I tried to get the "address" information …