A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

I'm really close with ps1a but can't work out where I'm going wrong.

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

    Here is my code: # establish initial variables candidate = 3 # 3 is the first number to be tested prime_count = 1 # prime count is 1 as prime 2 already counted divisor = 2 # initial divisor. # if the prime count is still less than 1000: while prime_count < 1000: # if there is no remainder... if candidate%divisor == 0: candidate = candidate + 2 # try a new candidate else: while divisor < (candidate/2): if candidate%divisor == 0: # if there is no remainder... candidate = candidate + 2 # try a new candidate divisor = 2 # and reset the divisor else: # if there is a remainder... divisor = divisor + 1 # try a new divisor prime_count = prime_count + 1 # if no divisor is found divisor = 2 # increase prime count by 1 candidate = candidate + 2 # reset divisor and try new candidate print "The 1000th prime number is",candidate When I run the module, I get the answer 7921, which is the 1001st prime number. Also my code could be better I think. Any ideas? Thanks.

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

    ah the comments make it look horrible, sorry about that.

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

    Because you run from 0 -1000. Try 999 as limit.

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

    with 999 as the limit i get 7909, which isnt even a prime number apparently.

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

    Hmmm...I had the same problem when I did it....gimme sec.

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

    Ok your equation is in error. I changed this to check: while prime_counter < 5: and added: print candidate, just under candidate = candidate + 2. As you see it returns 9 as prime but not 11.

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

    So instead, seeing as you are checking divisors in a range, I would use a FOR loop with a range of divisors to test for candidate%divisors == 0.

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

    def sums2(): candidate = 3 # 3 is the first number to be tested prime_count = 1 # prime count is 1 as prime 2 already counted divisor = 2 # initial divisor. # if the prime count is still less than 1000: while prime_count < 5: # if there is no remainder... if candidate%divisor == 0: candidate = candidate + 2 # try a new candidate else: while divisor < (candidate/2): if candidate%divisor == 0: # if there is no remainder... candidate = candidate + 2 # try a new candidate divisor = 2 # and reset the divisor else: # if there is a remainder... divisor = divisor + 1 # try a new divisor prime_count = prime_count + 1 # if no divisor is found divisor = 2 # increase prime count by 1 candidate = candidate + 2 # reset divisor and try new candidate print candidate print "The 1000th prime number is",candidate

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

    The above will test for a smaller range, and print the primes, as the prime counter increases.

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

    for anyone that is interested, my code was going wrong because it was printing the 1000th prime number + 2. This was because the print candidate command comes after candidate = candidate + 2, and so when the 1000th prime is reached the program takes it, adds 2 and then prints. With some help I have fixed it and now have a working program. :) Thanks guys.

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

    I am getting the correct answer/have the correct code for ps1a, but it takes forever to run the whole thing. Obviously, this early in the course, we're not expected to write perfectly optimized code, but I want to start thinking/writing code in that manner. Is it OK that this code takes forever to run?

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

    I wouldn't worry so much about speed as about making it do the right thing. If you get it to work, then go over your code and see how it can be improved upon. For example, I generally write at least 5 or 6 versions of the problems we face, using the simplest versions, to go to the next problem. If you see that it is taking exceptionally long to execute, then you can always break down your code into smaller chunks, and try redefining each step into something simpler, or smaller. Try to make your program do the same thing using different methods. Eventually you will find your own path and style.

  13. 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.