1

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.

4
  • 1
    Your indentation is awry there. Where is that else clause? Commented Jun 28, 2013 at 4:21
  • 2
    I think "break" is meant to be indented. Commented Jun 28, 2013 at 4:24
  • 2
    Tip: Almost always use xrange() instead of range(). Commented Jun 28, 2013 at 4:26
  • @JonathonReinhart For python 2 anyway (which this question is using) Commented Jun 28, 2013 at 4:26

2 Answers 2

2

Your logic was right, your blocks were not. The break should be indented (under if n % i) and the else should be unindented, so it's the else for the for loop - i.e. numbers are only added if NONE of the primes are it's factor, instead of once for EACH of the primes are it's factor.

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 can save time by only dividing n by (the list of primes so far), instead of all numbers - simply replace range(2, n) with primes

Sign up to request clarification or add additional context in comments.

10 Comments

I despise the for/else construct in Python.
@JonathonReinhart Any particular reason? It's a concise way to do something that otherwise generally requires an extra variable or function.
@Amber Because they re-used the else keyword, where they should have introduced another one. for ... andthen makes way more sense to me. Just reading it, I almost always get it backwards from the way it actually works (else executes iff for completes normally).
@Amber I like the concept, just hate the keyword. It's just quite different from an else associated with an if. There, the else body executes only if the if body doesn't execute. Before I read up on for..else, I assumed the else only executed if the for never executed once.
I don't tend do use it much, myself, but when I work with other people's code, I tend to apply the minimum reasonable change to fix the problem. That rule (of mine) applies in this case.
|
0

There were a couple of mistakes. Here is my version of your code

counter = 1
primes = [2]
n = 3
while counter < 1000:
    flag = 0
    for i in range(2, n):
        if n % i == 0:
            flag = 1
            break
    if flag==0:
        primes.append(n)
        counter = counter + 1
    n = n + 1

print primes

You can run the aboce code here http://codebunk.com/bunk#-Iy8aps3Zy7woREB64VF

EDIT: You do not need to check divisibility with numbers from 2 to n. Maybe change it to 2 to n^0.5

4 Comments

Thank you. It's good to know that my logic wasn't the problem. Why did you choose to lose the flag?
"Why did you choose to lose the flag?" - I didn't get what you are saying.
Should have said, "Why did you choose to USE the flag?"
TO signify that I the inner loop did not end naturally but rather I broke out of it, i.e., I found a number which divides n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.