I'm starting a computer science program in the fall and I'm trying to build my programming chops a little before starting.
I'm going through the MIT OCW 6.0 problems and the first is to produce the 1000th prime number. Obviously, I want to produce my own code, so I'm wondering where my logic is going wrong.
counter = 1
primes = [2]
n = 3
while counter < 1000:
for i in range(2, n):
if n % i == 0:
break
else:
primes.append(n)
counter = counter + 1
n = n + 1
print primes
You guys are amazing, so I won't explain every line here, but the gist of my logic is that I want this loop to start at n. If n is prime, add it to the list and add 1 to the counter, if not move on to the next number. Lastly, print the list, ending in the 1000th prime.
Look, I know this is "brute force" and I know there are Sieves out there and more complicated logic, but I want this to work in this way. Right now, I'm getting a lot of numbers repeated and no where near the 1000th prime.
Thanks guys. This is my first question, but I'm sure there will be more to come.