Solutions for finding the 1000th prime

N

Neal

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.
 
P

Peter Otten

Neal said:
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
 
N

Neal

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top