I’ve decided to take the MIT OpenCourseWare class 6-00 Introduction to Computer Science and Programming. I know, I know, it’s an intro class, but it looks like fun. That and I’ve never programmed in python before, which is the language taught in this course. So, I’m going to learn Python for fun. It’s amazing how quickly the language can be picked up. Here is my first real attempt at writing in the language. Other than a couple of test “hello world” type things to get a better grip on the syntax.

So, the first real problem given out is to write a program to find the 1000th prime number. So, here is my first go round. It’s not very elegant, or algorithmically pretty, but it brute forces the correct answer out to at least 10,000 prime numbers. I checked.

# Write a program that computes and prints the 1000th prime number

# initialize some variables

# generate odd integers

# check to see if it’s a prime number

# use a%b to get remainder and see if it equals zero

# Note, we count 1 as a prime instead of 2 just to make this easier to code.

# 2 is an even number and messing with the logic. It’s a cheat, I know.

count = 1

candidate = 1

while count <= 1000: # iterate until 1000 primes found

myresult = ‘prime’ # turn on prime found flag

half = candidate / 2 # set the half way point

iterator = 3 # start dividing with 3

while iterator <= half :

if candidate % iterator == 0 : # do we have a divider?

myresult = ‘notprime’ # turn off prime found flag

iterator = half + 1 # Stop counting we found a divider

iterator = iterator + 2

if myresult == ‘prime’ :

count = count + 1 # found a prime, iterate the counter

candidate = candidate + 2 # set candidate to next odd number

# Print out 1,000th Prime Number

print ‘The’,count – 1,’th prime is ‘, candidate – 2

And in case you are wondering the answer is

The 1000th prime is 7919

## 6 comments

Comments feed for this article

January 3, 2010 at 5:40 PM

John Leidelsilly interpreted languages with no datatypes 🙂

Python is definitely cool. I’ve never taken much time to learn it though. No where did I put those PERL books…. ?

January 3, 2010 at 7:52 PM

icehawk55It doesn’t help that the formatting gets lost using wordpress. Turns out Python uses formatting to control the loops instead of keywords, well, kind of a combination.

I agree, PERL is the language to use, but python is fun to learn.

January 5, 2010 at 1:51 PM

StephanieHappy Birthday Rich! I hope you have a great one!

July 18, 2010 at 1:00 PM

JohnWriter,

I am currently trying to teach myself programming though the MIT open courseware and I am having a terrible time trying to understand what I am supposed to be doing. The first lecture and assignment was very easy to understand. Then, it seemed as if we skipped 1,000,000 chapters. You seem to have a strong grip on programming. That being said, what free resource could I use to learn programming? (keep in mind that I have ZERO computer science background)

Thanks

July 19, 2010 at 9:41 PM

icehawk55I’m not sure what to tell you John. Like you I don’t have a compsci background so I just teach myself. I hit misc tutorials online and go through the ones that look interesting.

Sorry I can’t be more helpful than that.

You’re right about there being something missing in the middle of the python class. It does jump big time. Which is a bit odd.

October 20, 2011 at 6:46 AM

ZhiAnhello dear sir, i came about your code when i was trying my hand on python, i realised that your code presents some problem, for instance it treats 1 as a prime number when it shouldn’t have been, and it skips 2 as a prime number. so i think a modification should be changing candidate to an initial value of 2, changing iterator to an initial value of 2, and candidate=candidate+1 instead of +2. not sure if you still care but just thought i’ll type it out here 🙂 thanks for your help!