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 02:29 am (UTC)
zulu: (house - hesitant)
From: [personal profile] zulu
#time 12:17 - 2:26
# the 1000th prime is 541

poprime = 3 # start with 3 as the first potential prime to get 2 out of the way
counter = 1 # since 2 is prime start the iteration already at 1


while counter < 1000: # begin by checking how many iterations we're at
      if (poprime/2)*2 == poprime: # if poprime is even
            poprime = poprime + 1 # if poprime is even, add one and restart the iteration
      elif poprime%2 > 0: # else if poprime is odd
            factor = 2 # start factors at 2 because a prime is divisible by 1
            while 1 < factor < 0.5*poprime: # all factors greater than half are mirrors
                  if poprime%factor == 0: # no remainder means it's divisible
                        poprime = poprime + 1 # not prime; increase poprime but not counter
                        factor = poprime # this should make the factor too big to get into this loop again
                  if poprime%factor > 0: # in this loop must check all factors
                        factor = factor + 1 # if one factor doesn't work, check the next
            if factor >= 0.5*poprime: # when you've checked all the factors
                  if poprime%factor == 0: # no remainder means it's divisible
                        poprime = poprime + 1 #not prime; increase poprime not counter
                  if poprime%factor > 0: # 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, then having checked all possible factors should be enough.
                  # This number is prime.
                        poprime = poprime + 1 # Increase poprime to check the next potential
                        counter = counter + 1 # You found a prime, so increase the counter.
            poprime = poprime +1

if counter == 1000:
      print poprime


This doesn't work, but it's based only on what we've been taught in the lectures and maybe some of the readings. I don't understand lists yet. I can't read the code in the answers upthread of mine--I was looking for things to reverse-engineer, but no such luck!

What is nice about this program is that by pure chance it'll give you 29 as the 10th prime if you set the counter to print at 10. It makes you feel all happy inside until you try it at different numbers!
Edited Date: 2009-11-16 02:41 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 08:03 pm
Powered by Dreamwidth Studios