MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
# Problem Set 1a
# Name: MORAYI
# Collaborators: Curious Reef: duallain
# Time: 2+ days
#
from math import log
primeCandidate = 3
primeCounter = 1
primes = [2]
logSum=0
primeLimit = int(raw_input('Please enter upper limit of primes: '))
def isprime(n):
for x in primes:
if n % x == 0:
return False
return True
while ( primeCounter < primeLimit ):
if isprime(primeCandidate) == True:
primes.append(primeCandidate)
primeCounter += 1
primeCandidate += 2
else:
primeCandidate += 1
#####
# Problem Set 1b
# Name: MORAYI
# Collaborators: Curious Reef: duallain
# Time: 0:30 min
n = primes[-1]
for x in primes:
if x <= n:
logSum = logSum + log(x)
print primes
print "the sum of log(prime) where prime <= n = ", logSum
print "n = ", n
print "The ratio of logSum/n = ", logSum/n
morayi
6 months ago