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!
medrin: matlab code with everything but 'hold on' blurred (Default)

[personal profile] medrin 2009-11-20 03:35 pm (UTC)(link)
My solutions for #1

1a)
from math import*

def primenr(nr):                #input, the n:th prime
    primecount=1                #primecount (number of identified primes)
    n=1                         #starts at 1 because 2 is a prime
    while primecount < nr:      #loop till we find the muber of primes we want
        n+=2                    #since only odd nr are prime, add 2 at the time
        if isprime(n):          #returns True if n is a prime
            primecount+=1       #We found another prime!
    return n                    #return the wanted prime

def isprime(n):                 #checks if it's a prime, returns True,
    prime=True                  #if not, returns False
    for i in range(2,int(sqrt(m))):      #goes throug all possible divisors
        if n%i == 0:            #up to the square root. (no possible higher)
            prime=False         #if it finds a divisor, it's not a prime
    return prime


ans=primenr(1000)
print ans

1b)
from math import*

def isprime(m):         #retyrns true if prime, otherwise false.
    prime=True          #same as in ps1a
    for i in range(2,int(sqrt(m))):
        if m%i == 0:
            prime=False
    return prime

def logratio(n):
    sumlog=log(2.0)         #start with adding the log for 2
    for m in range(3,n+1):  #finds all the primes between 2 and n
        if isprime(m):
            sumlog+=log(m)  #adds the log of the prime to the sum.
    print sumlog, m, sumlog/m 

logratio(1000)