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-19 07:12 pm (UTC)
erda: (Default)
From: [personal profile] erda
I'm learning a lot by looking at everyone's solutions after working out my own much less sophisticated ones.

Here are mine. I stuck to while and if statements, since those are about the the only things I understand so far.

Problem 1:

count=2 #keeps track of which prime number we are at, assume 2 is the first prime
candidate=3 #the number we are checking to see if it is prime
test_divisor=3 #the number we are dividing by
while count<1001: #program will continue until we reach the 1000th prime
while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate
remainder=candidate%test_divisor
if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate
candidate+=2 #only odd numbers need to be checked
test_divisor=3 #must reset this for next candidate
else:test_divisor+=2 #if there is a remainder we will try the next divisor, no need to use even divisors
count+=1
test_divisor=3
candidate+=2
print "The 1000th prime number is", candidate-2

Problem 2:

from math import *
n=input("What number do you want to stop at?")
candidate=3 #the number we are checking to see if it is prime
test_divisor=3 #the number we are dividing by
OldLog=log(2)
while candidate<=n: #program will continue until we reach the input number
while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate
remainder=candidate%test_divisor
if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate
candidate+=2
test_divisor=3 #must reset this for next candidate
else:test_divisor+=2 #if there is a remainder we will try the next divisor
NewLog=OldLog + log(candidate)
OldLog=NewLog
test_divisor=3
candidate+=2
print "Your number is", n
print "The sum is", OldLog
print "The ratio of the sum to your number is", OldLog/n

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 04:48 pm
Powered by Dreamwidth Studios