elz: (ada-tubes)
[personal profile] elz posting in [community profile] intro_to_cs
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!

Date: 2009-11-16 06:27 am (UTC)
zulu: (muppets - inspired)
From: [personal profile] zulu
Triumph, dudes! The key was really seeing how it worked on the much simpler math of finding a certain number of odds. This works now! Still probably a million times clunkier than some, but I've never programmed before in my life and using what we've learned so far I got to this point. I've documented it a lot so that y'all can see what I did and why. Also, I apologize to [personal profile] elz's inbox, where all my edited comments full of code probably ended up.

# 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)
Edited Date: 2009-11-16 06:32 am (UTC)

Profile

Introduction to Computer Science

July 2010

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526272829 3031

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 12th, 2025 03:11 pm
Powered by Dreamwidth Studios