Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

JonathanFichter

  • 3 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
  1. JonathanFichter
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  2. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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

  3. ewsmith
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. JonathanFichter
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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/

  5. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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/

  6. JonathanFichter
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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!

  7. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  8. JonathanFichter
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  9. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  10. JonathanFichter
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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?)

  11. bwCA
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    yep http://docs.python.org/tutorial/controlflow.html#documentation-strings

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

    • Attachments:

Ask your own question

Ask a Question
Find 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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.