A community for students.
Here's the question you clicked on:
 0 viewing
JonathanFichter
 4 years ago
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
JonathanFichter
 4 years ago
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

This Question is Closed

JonathanFichter
 4 years ago
Best ResponseYou've already chosen the best response.1Just 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.

bwCA
 4 years ago
Best ResponseYou've already chosen the best response.1include "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#modulemath for flow control: http://docs.python.org/tutorial/controlflow.html#morecontrolflowtools the docs are probably on your computer also  i find that they are easier to search through

ewsmith
 4 years ago
Best ResponseYou've already chosen the best response.0Another, 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.

JonathanFichter
 4 years ago
Best ResponseYou've already chosen the best response.1Thanks! 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/

bwCA
 4 years ago
Best ResponseYou've already chosen the best response.1does 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/

JonathanFichter
 4 years ago
Best ResponseYou've already chosen the best response.1Thanks 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!

bwCA
 4 years ago
Best ResponseYou've already chosen the best response.1just 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.

JonathanFichter
 4 years ago
Best ResponseYou've already chosen the best response.1For 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 exceptionforthenumber2 stuff as a function that primeTest calls (so that people would see what primteTest can do as a standalone 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.

bwCA
 4 years ago
Best ResponseYou've already chosen the best response.1well, 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 reuse 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.

JonathanFichter
 4 years ago
Best ResponseYou've already chosen the best response.1Sounds good! Thanks for the idea about noting any assumptions or limitations (if you had to make them) in the docstring. (Is a docstring the triplequoted stuff that follows the definition of a function?)

bwCA
 4 years ago
Best ResponseYou've already chosen the best response.1yep http://docs.python.org/tutorial/controlflow.html#documentationstrings
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.