A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Finally problem 1 finished!! # Finding up to the 1000th prime x = 0 count = 0 div = [] primes = [] while count <= 1000: if x > 1 and not(x%2) == 0: for i in range(1, 1+x): if x%i == 0: div += [i] if div[1] == x: primes += [x] count += 1 x += 1 del div[:] However, I don't know how to account for 2.. Also, please critique the code. Thanks

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    just add 2 to primes before you loop. If you're taking advantage of the fact that primes aren't even you're using some apriori knowledge of the problem, and doing so also implies that 2 is a prime.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    other things, try to avoid using variables like i, and x. Instead name them things that make sense i is a possibleDivsor, x is a possiblePrime. Also div = [] is a bit nicer than del div[:] in terms of expressing what you mean. Just move the initialization of div into your while loop at the top and you won't have to delete it at the bottom of the loop.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Cool, thanks polak

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.