anonymous
  • anonymous
PS1, 1a: Think I'm close but stuck and need some help! It looks as if my program is spitting out some non-prime numbers as prime. The first instance I find is when my program suggests that the 61st prime number is 289 (it's not prime). Where am I going wrong? http://dpaste.com/524739/
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.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
I would suggest decomposing the task into two separate parts. First, write some code that, given some static assignment of candidate = 114321 or some other number, simply tests to see whether or not this candidate variable is a prime number. Then, once that's completed, take that code, and place it in the body of a while loop that increments candidate until plimit primes are found.
anonymous
  • anonymous
Sounds good. I'll give that a shot now. Thanks!
anonymous
  • anonymous
Ok, so I think I was able to get a test for primeness. Just don't think it is the most efficient way (given the tools given to date). Any advice much appreciated! Will now work on integrating the while loop that increments candidate until plimit primes are founds. http://dpaste.com/524753/

Looking for something else?

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

More answers

anonymous
  • anonymous
Man, I just spent nearly an hour trying to figure out what wasn't working in my program and it was just a missing indentation! Seem to have it working. polpok (or anyone else), does this look right to you? Something tells me it may be a bit clunky. http://dpaste.com/524758/
anonymous
  • anonymous
Oh, and polpok, thanks again for your help. I really appreciate it!
anonymous
  • anonymous
Sorry bout that. I was off working on something else. I'm going to forgo analysis of your final code for a moment and look at what you wrote for the simple test of primeness. I've made a few minor edits to clean things up and make it a bit easier to read, but the logic is still the same. http://dpaste.com/524760/ Now, if we take your code for looping until we find plimit primes and change the prompt for candidate into a manual assignment we have http://dpaste.com/524764/ Which seems like a perfectly elegant solution.
anonymous
  • anonymous
The key here is to envision what is to be done for a single number, then utilize that process over a sequence of many numbers. Once you have formalized the process for performing the task once, you shouldn't need to change anything about it to perform it many times (in a loop) other than changing how your input variables are initialized.
anonymous
  • anonymous
That makes sense. And I imagine breaking it down before building it up helps avoid a lot of potential errors. Looking over your clean-up of my code was helpful. Particularly converting the input into an integer so I don't have to do it within the loop. Great tip. One question on loops. I see you took candidate=candidate+1 and put it outside of the innermost while loop. For my solution, I incremented in two spots (see below). Is your way just saving a line of code or is it more important than that? I'm having trouble understanding why it increments without being explicitly told to at the end of both if statements. while (divisor<=(candidate/2)) and (not_prime==False): if candidate%divisor == 0: #if no remainder, not prime not_prime=True #not prime candidate = candidate + 1 #not prime, test next candidate divisor = divisor+1 #test next divisor if not_prime==False pcounter=pcounter+1 #add 1 to prime counter candidate = candidate + 1
anonymous
  • anonymous
Remember that the responsibility for assigning candidates is not part of the process of testing for primeness. My code is organized the way it is to provide a clear distinction between the two tasks. The innermost task is simply to take the given candidate and see if it is prime. The outer loop is responsible for iterating through all the possible candidates and assigning each one so that we can test them. This clear distinction makes it much easier to debug in that I can ensure that the prime test code is working correctly before I add the outer loop. But that debugging is only useful if I do not change the prime test process when I add it to my loop for candidates. Otherwise I have to go back and re-debug/test everything. The process of your algorithm is logically equivalent since the number was either prime or not. And in both those cases you are incrementing your candidate. The difference is one of expressing what you mean. For your version you're saying, "If it's prime, move on to the next candidate. However, if it's not prime move on to the next candidate". For my version, I'm saying "Always move on to the next candidate". Since my choice of candidates is not dependent on primeness, I can reuse my code easily to find all primes between any two integers where the distance between the primes should be greater than a third integer. And again, nothing about the code I used to test for primeness needs to change. To emphasize this, I've added comments to show where exactly this unchanging portion of my algorithm is. And you'll note that this code is identical to what I used in the previous 2 instances to test a single number, and to find the 1000th prime. http://dpaste.com/524771/
anonymous
  • anonymous
have you gotten anywhere on Pset 1b? i'm stuck on adding them all together
anonymous
  • anonymous
i actually haven't started 1b yet. just read through it. probably gonna get cranking on it this afternoon. i watched lecture 3 which provides a technique i think you can use but my guess is there's a way to do it without that which i am going to try to figure out if i can.
anonymous
  • anonymous
yea i wasn't trying to jump ahead just yet. I wanted to do the project with what i've been taught so far. I've got the understanding for it just can't seem to put it together just yet.
anonymous
  • anonymous
my plan is to tackle the problem the way polpok suggested i work through 1a. mainly chopping it up into discrete steps. i already have code that determines primeness. and i already have a way to limit the number of primes i want to work with. so i need to create code that will take the log of any number x which shouldn't be too difficult. finally, i need to figure out a way to sum the resulting logs together (sounds like this is where you ran into trouble and i imagine i will too). gonna start in about an hour. wish me luck!
anonymous
  • anonymous
haha good luck mate. don't forget to put from math import* at the top of your code otherwise it won't recognize log.
anonymous
  • anonymous
so what i got it to do was take the ln of each prime number and add them together. but i didn't type ln in the code i wrote log(number).. is that what they wanted?
anonymous
  • anonymous
By default the log function uses the base e. So your code is correct. As an aside, please (always) avoid using from module import * Instead import what you want explicitly, or access the module's namespace indirectly. from math import log,e #explicitly import log print log(e) import math #import the module only print math.log(math.e) #access the attributes of the module indirectly
anonymous
  • anonymous
thanks for the clarification. pretty excited to have figured something out on my own
anonymous
  • anonymous
link to my solution to 1b is below. polpak, is there a reason you print log(e) and math.log(math.e)? i also used "from math import *", probably because that's what is listed in the problem set. what is the reason for the specificity? to avoid importing a ton of stuff unnecessarily? i will go back and make the edit in my program. thanks polpak! http://dpaste.com/524997/
anonymous
  • anonymous
when you do it like that does it include log(2) in with the answer? I had to manually do that.
anonymous
  • anonymous
it does. polpak tweaked my original attempt at 1a so that it counts 2 as prime. i believe it is because i initiate my variable not_prime as False and my divisor variable as 2. my second while loop reads, "divisor <= candidate/2" so in the case of candidate=2 and divisor=2, that doesn't hold. so it jumps to the next if statement and counts it as prime. is that right polpak?
anonymous
  • anonymous
Hey guys, I'm doing the same problem and wanted to remark that--unless I'm mistaken--you don't actually need to check divisors up to candidate/2. Instead, it should suffice to check up to Sqrt(candidate). This would probably save a good bit of time in case you're checking very large numbers, right? Thanks a lot for sharing your ideas! :)
anonymous
  • anonymous
i guess that's right! would certainly save computation time. thanks for pointing out keinerniemand! i'm going to update my script.

Looking for something else?

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