Problem Set 1 b

#Problem 2.
#Write a program that computes the sum of the logarithms of all
#the primes from 2 to some number n,
#and print out the sum of the logs of the primes,
#the number n, and the ratio of these two quantities.
#Test this for different values of n.
#You should be able to make only some small changes to your
#solution to Problem 1 to solve this problem as well.
#Name:Jeff Chen
#@Date:2012-6-23

from math import *
n=0
num=1
Sum=0
while n<=1000:
    isprime=True
    for j in range(2,int(sqrt(num))+1):
        if num%j==0&num!=j:   #Judge whether it is a prime
            isprime=False
            break
    if isprime:
        n=n+1
        Sum+=num     #add them together
    num+=1
##print out 
print "The "+str(n-1)+"th prime is "+str(num-1)
print "The sum of them is "+str(Sum)
print "The ratio of sum and "+str(n-1)+" is "+str(Sum/(n-1))

posted on 2012-06-23 18:30  X.P.Chen  阅读(163)  评论(0编辑  收藏  举报

导航