At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
Paste your code using dpaste, pastebin, git, etc. and ask people ehre to look at it.
good question and nice reply
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.')
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.
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.
Can I post in HTML?
dpaste works well if you don't want to make an account somewhere. http://dpaste.com/hold/1308510/
http://dpaste.com/1317567/ Thanks, e.mccormick and happybee. Looks like I could have shortened my code a bit!
Well, that was for something else.... but yah, there are ways to do primes easily. I may have the first 1000 code somewhere.
I also need to continue with the lectures, because I'm not sure what the def functions does yet.
def = define
Basically you could loop through something like that and count how many you find.
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!
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.
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.
Thanks for the catch on add 2 to div when testing for prime numbers.
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.
@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.