Poll & solutions
Nov. 11th, 2009 10:52 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png)
First:
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!
Open to: Registered Users, detailed results viewable to: All, participants: 25
Tickyboxes!
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!
no subject
Date: 2009-11-16 06:27 am (UTC)# primes
# AKA sleep is for the weak
# This script will calculate the nth prime number
primes = [2] # This is a list called primes, and its first item is 2
counter = 1 # Because 2 is the first prime and your list already has 1 item.
print ("Let's find the nth prime number. Please enter a postive integer: ")
answer = raw_input()
nth = int(answer) # The user enters the answer N. To do math with the answer, you have to make it an integer.
prime = 3 # Start at 3 so you don't have to deal with the fact that 2 is the first prime.
while counter < nth: # Until you reach N, follow these instructions:
if nth == 2: # So you don't have to deal with 2 being the first prime
primes.append(3) # Make sure you keep your list up-to-date otherwise nothing else will work
print ("The ") + str(len(primes)) + ("nd prime number is 3.") # Concatenating strings & integers is fun!
break # I don't know if this is necessary, but it gets you out of the loop.
elif 2 < prime and prime % 2 == 0: # Here's where the real work starts. If the potential prime is even:
print str(prime) + (" is even. It doesn't go on the list.") # You don't go further.
print ("We have found ") + str(counter) + (" prime numbers. Let's keep going.")
prime = prime + 1 # Having dealt with one case, make sure you increase the iteration
elif 2 < prime and prime % 2 != 0: # If the potential prime is odd:
print str(prime) + (" is odd. Let's keep working with it.") # There's more to do.
factor = 2 # Start with a factor of 2 because primes are divisible by 1.
while factor <= prime*0.5: # Once you get to half the number, the rest of the factors are mirrors.
if prime % factor == 0: # If it's divisible...
print str(prime) + (" has a factor. It is not prime.")
prime = prime + 1 # Increase the iteration but not the count of prime numbers
# (You haven't found any prime numbers yet.)
break
elif prime % factor > 0: # If it's not divisible...
print str(factor) + (" is not a factor. Let's keep going.")
factor = factor + 1 # You've tried one factor, now try the next.
if factor > prime*0.5: # You need to know that all your tests failed.
# But if you get out of your loops every time a test was true,
# then if your loops are good,
# so having checked all possible factors should be enough.
primes.append(prime) # You have found a prime number! Add it to your list.
if len(primes) == int(nth) and len(primes) == 3: # If you've reached N, print it out.
print ("The ") + str(len(primes)) + ("rd prime number is ") + str(prime) # This is just fancy to get the ordinal correct.
break
if len(primes) == int(nth): # If the number of items on your list is equal to N, then stop.
print ("The ") + str(nth) + ("th prime number is ") + str(prime)
break
else: # If you haven't reached N, keep going.
print ("We have found ") + str(len(primes)) +(" prime numbers. Let's keep going.")
prime = prime + 1 # Increase the potential prime by one.
counter = counter + 1 # Since you've found a prime, increase the count by one.
if nth == 1:
print ("The 1st prime number is 2.") # This helps deal with users who enter 1 as N. (Very funny, users. Not.)
if nth == 1000: # To shorten the program run time, comment out all that printing and just leave this one.
print ("The ") + str(nth) + ("th prime number is ") + str(prime)