Python nth prime

• Aug 16th 2011, 02:12 PM
dwsmith
Python nth prime
I have only been using Python for a few days.

I am trying to write a code in Python to calculate the nth prime number. However, I don't know how to start. The only thing I have written is:

def nthPrime(n):

I need some help on what to do next.
• Aug 17th 2011, 12:06 AM
CaptainBlack
Re: Python nth prime
Quote:

Originally Posted by dwsmith
I have only been using Python for a few days.

I am trying to write a code in Python to calculate the nth prime number. However, I don't know how to start. The only thing I have written is:

def nthPrime(n):

I need some help on what to do next.

One wayof doing this is to build up a list of primes until you have the required number.

So you may start with a seed list 2,3,5,7,11 (however long you want)

Then for you check integers sequentially for divisibility by the primes less than their square root, if not divisible by any of these then add to the list of primes.

This can be made more efficient by only checking odd numbers, and with added complexity elliminating other known composites from the search.

CB