素数,对数,求和,循环

时间:2015-02-22 19:44:52

标签: python loops sum primes logarithm

我试图制作一个程序来计算小于给定数字的所有素数,以及所有这些素数的乘积的自然对数。因此,因为我们正在使用对数,所以我还可以添加每个素数的自然对数小于给定数字。所以我创建了这个程序,但是我认为对数的表现有点怪异吗?我试着检查我的程序给出的总和是否正确,但首先我的程序只给出了整数作为答案,其次我的计算器给出了不同的答案。我的代码有问题吗?素数工作正常,但如前所述,对数是错误的。请参阅下面的代码

def prime_program():
while True:

    answer = raw_input("Do you want to know the primes smaller than a number n and the natural logarithm of the product of those primes? Please answer yes or no    ")

    if answer == "yes":
        n = float(raw_input("Please enter a whole number bigger than 1 ")) 
        counter = 1
        prime = 3.0
        log_of_prime_sum = float(log(2.0)) #I'm starting out with log(2.0) cause 2 is the first prime not counted later



        if n > 2.0:
            print "these are the primes smaller than %d" % n
            print 2.0 #cause I want to see all the primes but only want to check the odd numbers

            while prime < n:

                for x in range(2,int(sqrt(prime))+1): #range only works with integers right?

                    if prime%x == 0.0:       
                        break#because if it isn't a prime I don't want to do anything with it
                else:
                    print(prime) #cause I want to know the primes
                    log_of_prime_sum += float(log(prime)) #cause I want to add all the logs of the primes

                prime += 2.0 #we're starting with three, which is an odd, 
                             #and I'm adding two because I only want to check the odd numbers,
                             #because no even number other than two is prime

            print "the natural logarithm of the product of all the primes smaller than %d is %d"  % (n, log_of_prime_sum)
            ratio = float(float(n)/float((log_of_prime_sum)))
            print "The ratio of %d to the sum of the natural logarithms smaller than %d is %d" % (n, n, ratio) #here I want to print the ratio of n and the log_of_prime_sum
        else:
            print "There is no prime smaller than 2"
    elif answer == "no": #like what why would anyone open this file if he wouldn't want to do this lol
        print "well ok then"
    else:
        print "I'm sorry that was not a valid answer, you can only answer yes or no"
        continue #jumps back to while True:
    return #finished; exit the function
        #here I want to go back to answer = raw_input

prime_program()

这是我在windows powershell中的输出

Do you want to know the primes smaller than a number n and the natural log
arithm of the product of those primes? Please
answer yes or no    yes
Please enter a whole number bigger than 1 8
these are the primes smaller than 8
2.0
3.0
5.0
7.0
the natural logarithm of the product of all the primes smaller than 8 is 5
The ratio of 8 to the sum of the natural logarithms smaller than 8 is 1

1 个答案:

答案 0 :(得分:1)

虽然你的程序非常混乱(并且确定数字是否为素数的方法远非最佳,但可能存在一个错误。)

log_of_prime_sum += log_of_prime_sum+float(log(2.0))

+=意味着你&#34;增加&#34;带有操作数的变量,所以你声明:

log_of_prime_sum = log_of_prime_sum+log_of_prime_sum+float(log(2.0))

防止出现此类错误的一种方法是初始化(在程序的顶部,log_of_prime_sum已经使用float(log(2.0)),它甚至会提高性能你的算法很少。

换句话说,你加倍log_of_prime_sum ...

但是我会建议你让代码更具可读性并使用更快的质检。例如,您可以停止在 log(n)中使用 n 列举您想要检查的数字来列举潜在的分隔符,这样就可以读取:

for x in range(2,int(sqrt(prime))) :
    if prime%x == 0.0:
        break

最好使用int进行素数计算,只有在您希望将其添加到float时才转换为log_of_prime_sum

如果在终端中运行该程序:

import math

prime = 3
log_of_prime_sum = math.log(2.0) #I'm starting out with 0.0 cause the sum of the log of the primes should begin here
n = 8

print("these are the primes smaller than {}".format(n))
print(2)

while prime < n:
    for x in range(2,int(math.sqrt(prime))+1): #range only works with integers right?
        if prime%x == 0.0:       
            break
    else:
        print(prime)
        log_of_prime_sum += math.log(prime)

    prime += 2

print("the natural logarithm of the product of all the primes smaller than {} is {}".format(n, log_of_prime_sum))
ratio = (n)/(log_of_prime_sum)
print("the ratio is {}".format(ratio))

结果是:

$ python3 primefilter.py 
these are the primes smaller than 8
2
3
5
7
the natural logarithm of the product of all the primes smaller than 8 is 5.3471075307174685
the ratio is 1.4961359864267718