ps1a.py
#lists prime numbers up to the 1000th prime
def testPrime(x):
factor = 2
while factor**2 <= x:
if x % factor == 0:
return False
else:
factor = factor + 1
return True
candidate = 3
numPrime = 1
while numPrime < 1000:
if testPrime(candidate):
numPrime = numPrime + 1
candidate = candidate + 2
print "The 1000th prime number is", candidate - 2
ps1b.py
#asks for an upper limit
#calculates all primes and the sum of their logs up to the upper limit
#prints out the sum of the logs, the upper limit,
#and the ratio between the two
from math import *
def testPrime(x):
factor = 2
while factor**2 <= x:
if x % factor == 0:
return False
else:
factor = factor + 1
return True
candidate = 3
primeLogSum = log(2)
n = int(raw_input("What is n? "))
while candidate < n:
if testPrime(candidate):
primeLogSum = primeLogSum + log(candidate)
candidate = candidate + 2
print primeLogSum, n, primeLogSum/n
No comments:
Post a Comment