A community for students.
Here's the question you clicked on:
 0 viewing
bexica
 2 years ago
Is there a database of solution sets for the problems? I just finished problem set 1 for the 2008 course. I feel confident I got the right answer I'm just wondering how I did style wise to see if I got the answer efficiently.
bexica
 2 years ago
Is there a database of solution sets for the problems? I just finished problem set 1 for the 2008 course. I feel confident I got the right answer I'm just wondering how I did style wise to see if I got the answer efficiently.

This Question is Closed

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Paste your code using dpaste, pastebin, git, etc. and ask people ehre to look at it.

happybee
 2 years ago
Best ResponseYou've already chosen the best response.0good question and nice reply

bexica
 2 years ago
Best ResponseYou've already chosen the best response.0Here's what I got for problem set 1, part A. (Find the 1000th prime). # The first prime number is 2. # (The only even prime number is 2. All other even numbers can be divided by 2.) x = int(2); prime_count = int(1); print(x, ' is the 1st prime.') # Find the 1000th prime number while prime_count < 1000: x = x + 1 if x%2 != 0: # Do not consider even numbers (i.e., x%2 yields 0 remainder) div = int(3) # Start with a divisor of 3 while div < x: # Test for division by all non"x" numbers less than x test = (x % div) == 0 div = div + 1 if test: break # If test is true, x has an integer divisor besides itself if div == x: prime_count = prime_count + 1 print(x, ' is the ', prime_count, ' prime.')

bexica
 2 years ago
Best ResponseYou've already chosen the best response.0There were probably some short cuts I could have taken, as this is a bit of an exhaustive way to find primes, but it works. Basically, I check if a number x is even, if it isn't I test integers less than x to see if x can be divided by the number without a remainder. Then I break if a divisor is found. If no divisor is found I increase the prime count by 1.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2What this becomes if copied and pasted: # The first prime number is 2. # (The only even prime number is 2. All other even numbers can be divided by 2.) x = int(2); prime_count = int(1); print(x, ' is the 1st prime.') # Find the 1000th prime number while prime_count < 1000: x = x + 1 if x%2 != 0: # Do not consider even numbers (i.e., x%2 yields 0 remainder) div = int(3) # Start with a divisor of 3 while div < x: # Test for division by all non"x" numbers less than x test = (x % div) == 0 div = div + 1 if test: break # If test is true, x has an integer divisor besides itself if div == x: prime_count = prime_count + 1 print(x, ' is the ', prime_count, ' prime.') Which is why the code pasting services are suggested.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2dpaste works well if you don't want to make an account somewhere. http://dpaste.com/hold/1308510/

bexica
 2 years ago
Best ResponseYou've already chosen the best response.0http://dpaste.com/1317567/ Thanks, e.mccormick and happybee. Looks like I could have shortened my code a bit!

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Well, that was for something else.... but yah, there are ways to do primes easily. I may have the first 1000 code somewhere.

bexica
 2 years ago
Best ResponseYou've already chosen the best response.0I also need to continue with the lectures, because I'm not sure what the def functions does yet.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Basically you could loop through something like that and count how many you find.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Actually, for finding 1000 primes, yours is not bad. You would have to wrap a lot around mine to get it to work for finding a specific number of primes. One thing I would point out though, your div = div + 1 is incorrect. See, you start by eliminating all evens, which is great, then test with 3, 4, 5, 6, and so on. If you do div = div + 2 you test with 3, 5, 7, and so on, eliminating retesting the evens that 2 has already eliminated!

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Also, print("spam","spam","spam","spam","eggs") will put a space where the ,s are, so no need to pad. And, you could change the end behavior if you wanted a less massive list.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Look at what I changed in yours: http://dpaste.com/hold/1317623/ It is one line shorter and a few less calls and other minor changes.

bexica
 2 years ago
Best ResponseYou've already chosen the best response.0Thanks for the catch on add 2 to div when testing for prime numbers.

e.mccormick
 2 years ago
Best ResponseYou've already chosen the best response.2Those are the small things that make it more efficient. And you prepped very well for that by eliminating any evens! So it was only a small change. The other things I changed are style related more than quality. You explicitly declared ints, which is not bad, where as I trusted the declaration process. You used the detailed a = a+1, and I used the shorthand a+=1. The only other one I saw as being odd was your use of a temp variable for the truth test. That is useful if it needs to be tested more than once.

bwCA
 2 years ago
Best ResponseYou've already chosen the best response.0@bexica  i did the 2008 course and have (my) solutions for all the psets  there are 2008 solutions posted around the web  here is one (haven't looked at those': https://github.com/sebrenner/Mit6.00OCWProblemSetSolutions be sure to try them out on your own and feel free to ask questions here this is pretty cool: http://pythontutor.com/ if you want to post code  please use a code pasting site:  http://dpaste.com  http://pastebin.com  https://gist.github.com/  http://pastie.org  http://codepad.org  http://ideone.com  http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste.
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.