Here's the question you clicked on:

55 members online
  • 0 replying
  • 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.

  • This Question is Closed
  1. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Paste your code using dpaste, pastebin, git, etc. and ask people ehre to look at it.

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

    good question and nice reply

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

    Here'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.')

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

    There 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.

  5. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    What 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.

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

    Can I post in HTML?

  7. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    dpaste works well if you don't want to make an account somewhere. http://dpaste.com/hold/1308510/

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

    http://dpaste.com/1317567/ Thanks, e.mccormick and happybee. Looks like I could have shortened my code a bit!

  9. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Well, that was for something else.... but yah, there are ways to do primes easily. I may have the first 1000 code somewhere.

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

    I also need to continue with the lectures, because I'm not sure what the def functions does yet.

  11. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    def = define

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

    Thanks!

  13. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Basically you could loop through something like that and count how many you find.

  14. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Actually, 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!

  15. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Also, 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.

  16. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Look 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.

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

    Thanks for the catch on add 2 to div when testing for prime numbers.

  18. e.mccormick
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Those 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.

  19. bwCA
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 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/Mit-6.00-OCW-Problem-Set-Solutions 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.

  20. 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