Solutions for finding the 1000th prime

Discussion in 'Python' started by Neal, May 21, 2010.

  1. Neal

    Neal Guest

    I'm doing the MIT OpenCourseWare class that this assignment hails from
    and I don't doubt that its a relatively common assignment. Upon
    searching for it for some ideas of why my program wouldn't work one of
    the top results is this a thread from this group full of derision and
    sarcasm. Believe me I understand that the people here don't want to do
    another person's homework but for someone who isn't going to be coding
    for a living or is a hobbyist like I am, there could be some useful
    information. Eventually I figured out what I was doing wrong, and I
    was hoping to add some insight. At this point in the lectures about
    all we've learned to deal with as far as commands are: while, if,
    else, elif and some boolean operators.

    I started defining three variables I would need:
    One to count how many primes I have
    Which number I'm currently checking to be a prime
    Current number I'm dividing by as my checker

    The assignment states that its easiest to check all odd integers > 1
    but not to forget that 2 is a prime, easiest probably just to start
    your counter at 1, I didn't but it took an extra 3 lines just to get
    my counter to 1, and then get to the next number I'm checking all
    without being in a usable loop.

    You start your loop with the stated condition, no need for it to run
    any more past counting the 1000th prime.

    You could have your program check to see if a number is divisible by
    every number less than itself and if so moving on to your next number.
    However you do know that any number isn't going to be divisible by
    another number in that sequence until it reaches itself divided by 2.
    If you keep in mind though that we are only dealing with odd numbers,
    you know that they will not be divisible by two, so instead we can
    move on to itself divided by 3.

    Every iteration of the loop when it finds a prime, needs to increase
    the count, move onto the next candidate you're checking, and redefine
    the divisor you're starting to check next time around. If a number is
    determined not to be a prime you need to move onto the next candidate
    and redefine your divisor (which is where I was stuck for a long time
    writing this program). If your not sure that the number is a prime or
    not a prime you simply need to redefine the divisor and continue the
    loop.
    Neal, May 21, 2010
    #1
    1. Advertising

  2. Neal

    Peter Otten Guest

    Neal wrote:

    > I'm doing the MIT OpenCourseWare class that this assignment hails from
    > and I don't doubt that its a relatively common assignment. Upon
    > searching for it for some ideas of why my program wouldn't work one of
    > the top results is this a thread from this group full of derision and
    > sarcasm. Believe me I understand that the people here don't want to do
    > another person's homework but for someone who isn't going to be coding
    > for a living or is a hobbyist like I am, there could be some useful
    > information. Eventually I figured out what I was doing wrong, and I
    > was hoping to add some insight. At this point in the lectures about
    > all we've learned to deal with as far as commands are: while, if,
    > else, elif and some boolean operators.


    I'd like to bring your attention to my contribution to the above-mentioned
    thread.

    http://mail.python.org/pipermail/python-list/2009-November/1226626.html

    I posted it late in the thread's life because like you I disliked the
    direction the "discussion" was taking.

    My answer is not so much about an efficient solution to the actual problem,
    but more about how to approach a programming problem that initially seems to
    be over your head.

    As a long time reader I can confirm that if you show that you have made a
    minimum effort to solve even "homeworky" problems you usually get
    constructive hints on comp.lang.python. There is also a mailing list

    http://mail.python.org/mailman/listinfo/tutor

    for the absolute beginner.

    Peter
    Peter Otten, May 21, 2010
    #2
    1. Advertising

  3. Neal

    Neal Guest

    You did provide a very constructive answer and I do apologize for
    generalizing the group or all the posts. And while the original poster
    did not seem to have made much of an effort, the tone of the initial
    response of that thread turns off anyone else who may be willing to
    make that effort.

    I also want people who may have forgotten to understand how early this
    problem shows up in an Introduction to Computer Science course. In
    this class its the second assignment, right after the obligatory
    'Hello World!' variant.

    I'm sure that while finding solutions and new keywords is integral to
    learning how to program in a language, defining the a function isn't a
    tool that has been introduced quite yet, as simple as it may seem.

    Thank you for the link to the mailing list as well.
    Neal, May 21, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert P. J. Day
    Replies:
    5
    Views:
    2,636
    mrdeath5493
    Jan 17, 2011
  2. mrholtsr

    Code for finding the 1000th prime

    mrholtsr, Nov 15, 2009, in forum: Python
    Replies:
    12
    Views:
    1,103
    Terry Reedy
    Nov 17, 2009
  3. Martjack Ecommerce
    Replies:
    0
    Views:
    517
    Martjack Ecommerce
    Dec 22, 2009
  4. Cal Who
    Replies:
    0
    Views:
    455
    Cal Who
    Jun 9, 2010
  5. Jeremy Fischer
    Replies:
    0
    Views:
    177
    Jeremy Fischer
    Jan 16, 2005
Loading...

Share This Page