Python Forum
calculate the 10001st prime number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculate the 10001st prime number
#4
thanks, these two improvements actually make a lot of difference in performance.
improved code:
def calcPrim(number):
    primList = []
    primList.append(2)
    value = 3
    while len(primList) < number:
        if checkPrim(primList, value):
            primList.append(value)
        value += 2
    primList.insert(0, 1)
    return primList

def checkPrim(primList, value):
    for prim in primList:
        if prim>sqrt(primList[-1]+2): break
        if value%prim == 0: return False
    return True

number = 10001
primList = calcPrim(number)
print('{}th prime = {}'.format(number,primList[number-1]))
old code: 5.741328477859497s to calculate
new code: 0.273015499114023s to calculate
Reply


Messages In This Thread
calculate the 10001st prime number - by topologh - Mar-30-2018, 10:59 AM
RE: calculate the 10001st prime number - by Zatox11 - Mar-30-2018, 05:30 PM
RE: calculate the 10001st prime number - by Zatox11 - Mar-30-2018, 07:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to find the next prime number FirstBornAlbratross 8 5,217 Aug-14-2023, 01:16 PM
Last Post: deanhystad
  Calculate the multiple of a number pythonuser1 7 4,728 Mar-27-2020, 02:22 PM
Last Post: deanhystad
  Prime number code ryfoa6 3 3,047 Mar-21-2020, 12:23 AM
Last Post: ryfoa6
  Trouble interpreting prime number code ryfoa6 1 2,383 Mar-20-2020, 03:47 PM
Last Post: stullis
  Prime number Python homework mkrisz98 11 5,925 May-10-2019, 05:23 PM
Last Post: mkrisz98
  how to find a next prime number? iamyourfather 2 6,685 Oct-01-2017, 04:21 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020