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!
bellerina: (Default)

[personal profile] bellerina 2009-11-19 05:47 am (UTC)(link)
Hmm... it would be really helpful if we actually had "TAs" in this community, i.e., someone to look through our code and tell us whether we chose an efficient way to do things or not (and why), particularly for those of us who don't have a programming background. I wonder if we can lure some experts in to help out?

My solutions also seem a little clunky, but I guess they work...

My solution for problem 1:

count = 2
numb = 3
n = int(raw_input("What prime are you trying to find?"))
while(count<=n):
    i = 2
    isprime = 1
    half = numb/2
    while(isprime > 0 and i < half):
        if numb%i != 0:
            i = i+1
        else:
            isprime = isprime-1
    if isprime == 1:
        if count < n:
            count = count + 1
            numb = numb + 2
        else: break
    else:
        if count <= n:
            numb = numb + 2
print numb,"is prime number",count


My solution for problem 2:

from math import *
n = int(raw_input("What number?"))
logprimes = [log(2)]
numb = 3
while(numb<=n):
    i = 2
    isprime = 1
    root = sqrt(numb)
    while(isprime>0 and i<=root):
        if numb%i != 0:
            i = i+1
        else:
            isprime = isprime-1
    if isprime == 1:
        logprimes.append(log(numb))
        if numb < n:
            numb = numb + 2
        else: break
    else:
        if numb <= n:
            numb = numb + 2

print "The sum of the logs is",sum(logprimes)
print "The number is",n
print "The ratio of the sum and n is",sum(logprimes)/n