MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# Problem 1
primes = [2] # The first of the Primes.
x = 3 # First number to test.
while len(primes) < 1000: # Check that we still haven't found the 1000th prime!
isprime = True # Assume the number IS prime. Test against it.
for prime in primes: # For all the primes;
if (x%prime) == 0: # check to see if the number is divisable by a prime. If it is, set isprime to False.
isprime = False
if isprime:
primes.append(x)
x = x+2
print "And the 1000th Prime is... *drumroll*", primes[-1]
# Problem 2
print "And now for some Prime logging!"
from math import *
someLog = 0
n = input("So, what should our value of 'n' be?")
for prime in primes:
if prime <= n:
someLog = someLog + log(x)
print "Sum of the Prime logs: ", someLog
print "Wait, what was 'n' anyway? ", n
print "And how are they related now? ", someLog/n
andrewmeyer
1 year ago