anonymous
  • anonymous
Anyone good with Python and square roots? I must not be writing in correctly what I'd like Python to do: in line 17, I'm trying to tell the program that it can stop checking divisors once the divisor is the size of the candidate's square root. Line 18 represents my previous work: it was asking the program to test more divisors than it needed to, but it was getting the job done! http://collabedit.com/n7gja
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.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
Just realized collabEdit probably wasn't the best way to go: someone changed my code a bunch, but now it's hard to see what the original was.
anonymous
  • anonymous
include "import math" at the top of the module then math.sqrt(x) http://docs.python.org/tutorial/stdlib.html#mathematics http://docs.python.org/library/math.html#module-math for flow control: http://docs.python.org/tutorial/controlflow.html#more-control-flow-tools the docs are probably on your computer also - i find that they are easier to search through
anonymous
  • anonymous
Another, faster way to test this that does not require importing math is to test if the square of the divisor is bigger than the candidate.

Looking for something else?

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

More answers

anonymous
  • anonymous
Thanks! That's actually exactly what I was aiming to do. Instead of pasting the code into CollabEdit, I'll try dpaste this time: http://dpaste.com/582437/
anonymous
  • anonymous
does it work? i think there may be a problem with line 39 i added a print statement to see what happens - http://dpaste.com/582591/ here's what i got: http://dpaste.com/582592/
anonymous
  • anonymous
Thanks for testing some corners for me! I think my code is still standing up to the test: It yields the correct 1000th prime and doesn't make the mistake of thinking 20 is a prime. I might need, though, to improve my documentation: it looks like you wanted to isolate my primeTest function but didn't grab all of the necessary code. Here's all the primeTest stuff as I isolated it: http://dpaste.com/582743/ I'd love to hear what you think!
anonymous
  • anonymous
just a curious observation: from your first post: stop checking divisors once the divisor is the size of the candidate's square root but you wrote line 18 as: stop checking divisors once the quotient of candidate and divisor is the size of candidate's square root when you write functions, you might want to get in the habit of having them work in any context - your primeTest function will only work in conjunction with the rest of the code in the module.
anonymous
  • anonymous
For the stuff about the divisor: Thanks for checking my documentation against the actual code there! I'll have to make that change. As for primeTest, you have a good point. Would it be better if I wrote the exception-for-the-number-2 stuff as a function that primeTest calls (so that people would see what primteTest can do as a stand-alone function and what it can do in conjunction with other functions)? Alternatively, I guess I could build all of that into the primeTest function itself. Originally, I was thinking this might make the function unnecessarily cluttered and harder to read.
anonymous
  • anonymous
well, i only made that comment because if you need a primeTest function in the future for a different problem/solution then you would have to rewrite what you have written. if you write a function that assumes something you should probably document it in a docstring. what you did is ok but it seems that one thing programmers like to do is re-use stuff without having to diddle with it too much. definitely need to keep readability in mind - and what it's going to look like to you 5 years from now.
anonymous
  • anonymous
Sounds good! Thanks for the idea about noting any assumptions or limitations (if you had to make them) in the docstring. (Is a docstring the triple-quoted stuff that follows the definition of a function?)
anonymous
  • anonymous
yep http://docs.python.org/tutorial/controlflow.html#documentation-strings

Looking for something else?

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