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."
Problem 1a
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.)