Results 1 to 2 of 2

Math Help - Python nth prime

  1. #1
    MHF Contributor
    Joined
    Mar 2010
    From
    Florida
    Posts
    3,093
    Thanks
    5

    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.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4

    Re: Python nth prime

    Quote Originally Posted by dwsmith View Post
    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
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Invalid syntax in Python
    Posted in the Math Software Forum
    Replies: 7
    Last Post: October 24th 2010, 05:29 PM
  2. Recurrence relations in Python
    Posted in the Math Software Forum
    Replies: 3
    Last Post: September 26th 2010, 06:47 AM
  3. python questions...please help!!
    Posted in the Math Software Forum
    Replies: 3
    Last Post: December 9th 2007, 08:06 AM
  4. Python-conditions!!!
    Posted in the Math Software Forum
    Replies: 1
    Last Post: December 8th 2007, 05:27 AM
  5. Python - Repetition (Iteration)
    Posted in the Math Software Forum
    Replies: 3
    Last Post: December 8th 2007, 04:36 AM

Search Tags


/mathhelpforum @mathhelpforum