Poll & solutions
Nov. 11th, 2009 10:52 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png)
First:
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!
Open to: Registered Users, detailed results viewable to: All, participants: 25
Tickyboxes!
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!
no subject
Date: 2009-11-16 02:29 am (UTC)#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!