anonymous
  • anonymous
I'm really close with ps1a but can't work out where I'm going wrong.
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
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.
anonymous
  • anonymous
ah the comments make it look horrible, sorry about that.
anonymous
  • anonymous
Because you run from 0 -1000. Try 999 as limit.

Looking for something else?

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

More answers

anonymous
  • anonymous
with 999 as the limit i get 7909, which isnt even a prime number apparently.
anonymous
  • anonymous
Hmmm...I had the same problem when I did it....gimme sec.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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
anonymous
  • anonymous
The above will test for a smaller range, and print the primes, as the prime counter increases.
anonymous
  • anonymous
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.
anonymous
  • anonymous
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?
anonymous
  • anonymous
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.

Looking for something else?

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