A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

I just finished ps1a.py. Would you be so kind as to look it over and criticize it?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Here it is:

    1 Attachment
  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Well, you do get the right answer. Good job.

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

    Well is it streamlined and efficient? Anything I can remove that isn't completely necessary or something I could change to make better?

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Say I'm looking for whether 7919 is a prime number. Until when should I look? I believe you kept looking until 7919, but actually you only need to keep looking until the math.sqrt(7919). It's an existing theorem.

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

    But how would I know what to take the square root of and search until without first computing the 1000th prime?

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I don't think you understood. For every "check" in your program, only look for divisors until the square root of check. I changed 1 line in your code and it goes much faster : added import math for divisor in range(2,math.sqrt(check)+1)

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    You can cut that in half by only checking odds up to square root: range(2,math.sqrt(check)+1,2)

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Oops. I separated the two, that doesn't work for yours

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    GabeF's idea works as long as you precise that 2 IS indeed a prime number, since your loop won't look for it

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    oh ok i understand the square root thing now.

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I removed corner cases, then check odds: if num != int(num): prime = False # non-integers are not prime elif num < 2: prime = False # 2 is the lowest prime elif num%2 == 0: prime = (num == 2) # The only even prime number is 2 else: testAgainst = range(3,int(sqrt(num))+1,2) # No need to check even multiples prime = True for val in testAgainst: if num%val == 0: # If any value divides the number evenly, it's not prime prime = False break

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Well, there are a billion ways to do such a problem =p For those that are interested, one of the best ways is called Sieve of Eratosthenes, though it is a little more complex

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    omg wow... before it took about 10 seconds now it does computes instantly x.x

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    No problemo. Become my fan xD

  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Do you have sample code solving this problem using Sieve of Eratothenes? I got it to work using the sieve, but it was slower. To find the 1000th prime, my code took 0.019 secs vs 1.367 secs using the sieve. Not asking to be a jerk...interested in whether or not I can reduce the calculation time (just for fun).

  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok for explanation sake lets say there is 10 books and you wanna divide them up for 3 people (which is 10books/3people) but for every person to have the same amount of books each person can only get 3 and then there will be one book that no one gets. The one no one gets would be considered the remainder. Also here is a link with a similar explanation but let me know if you need another explanation. http://www.homeschoolmath.net/teaching/md/not_exact_division.php

  17. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Oh my bad my computer keeps trippin out it posted this under wrong question

  18. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I'm not sure GabeF, you can probably Google a code. It is very popular.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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.