quartzpebble: (Default)
quartzpebble ([personal profile] quartzpebble) wrote in [community profile] intro_to_cs 2009-11-15 10:30 pm (UTC)

Problem 1a

I started with 3 so that I could only check odd numbers, though using a list like some others did might have been more elegant. I also only checked factors up to the square root.


from math import *

target_primes = 1000 #nth prime that we want to compute
current_no = 3
prime_counter = 2
i = 3

while prime_counter < target_primes:
prime_counter += 1
current_no += 2
i = 3

while i <= sqrt(current_no):
if current_no%i == 0:
current_no += 2
i = 3

else:
i += 2
print str(current_no) + " is the " + str(prime_counter) + "th prime."


(Now with better formatting here.)

Post a comment in response:

This community only allows commenting by members. You may comment here if you're a member of intro_to_cs.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting