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-20 03:35 pm (UTC)
medrin: matlab code with everything but 'hold on' blurred (Default)
From: [personal profile] medrin
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)

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. 13th, 2025 10:18 am
Powered by Dreamwidth Studios