elz: (ada-tubes)
elz ([personal profile] elz) wrote in [community profile] intro_to_cs2009-11-11 10:52 pm
Entry tags:

Poll & solutions

First:

Open to: Registered Users, detailed results viewable to: All, participants: 25


Tickyboxes!

View Answers

Finished lecture 2
24 (96.0%)

Finished problem set 1
15 (60.0%)



Also, it occurs to me that it might be handy to have a place to post/discuss solutions to the problem sets, to see what we can learn from each other in the absence of TAs. If you actually go to MIT and the problem sets are still the same, don't read these. ;)

Fire away!
quartzpebble: (Default)

Problem 1a

[personal profile] quartzpebble 2009-11-15 10:30 pm (UTC)(link)
I started with 3 so that I could only check odd numbers, though using a list like some others did might have been more elegant. I also only checked factors up to the square root.


from math import *

target_primes = 1000 #nth prime that we want to compute
current_no = 3
prime_counter = 2
i = 3

while prime_counter < target_primes:
prime_counter += 1
current_no += 2
i = 3

while i <= sqrt(current_no):
if current_no%i == 0:
current_no += 2
i = 3

else:
i += 2
print str(current_no) + " is the " + str(prime_counter) + "th prime."


(Now with better formatting here.)
quartzpebble: (Default)

Not actually better formatting

[personal profile] quartzpebble 2009-11-15 10:32 pm (UTC)(link)
Is there a way to keep the indents in my code, or do I need to replace tabs with spaces here? It looked fine before I posted. :P