A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
I just finished ps1a.py. Would you be so kind as to look it over and criticize it?
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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well, you do get the right answer. Good job.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well is it streamlined and efficient? Anything I can remove that isn't completely necessary or something I could change to make better?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Say 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0But how would I know what to take the square root of and search until without first computing the 1000th prime?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0You can cut that in half by only checking odds up to square root: range(2,math.sqrt(check)+1,2)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Oops. I separated the two, that doesn't work for yours

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0GabeF's idea works as long as you precise that 2 IS indeed a prime number, since your loop won't look for it

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0oh ok i understand the square root thing now.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I removed corner cases, then check odds: if num != int(num): prime = False # nonintegers 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Well, 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0omg wow... before it took about 10 seconds now it does computes instantly x.x

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0No problemo. Become my fan xD

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Do 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).

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Ok 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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Oh my bad my computer keeps trippin out it posted this under wrong question

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I'm not sure GabeF, you can probably Google a code. It is very popular.
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.