A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
I'm really close with ps1a but can't work out where I'm going wrong.
anonymous
 5 years ago
I'm really close with ps1a but can't work out where I'm going wrong.

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Here 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ah the comments make it look horrible, sorry about that.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Because you run from 0 1000. Try 999 as limit.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0with 999 as the limit i get 7909, which isnt even a prime number apparently.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hmmm...I had the same problem when I did it....gimme sec.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Ok 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0So 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0def 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0The above will test for a smaller range, and print the primes, as the prime counter increases.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0for 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.