【问题标题】:primes, logarithms, summations and loops素数、对数、求和和循环
【发布时间】:2015-02-22 19:44:52
【问题描述】:

我正在尝试编写一个程序来计算所有小于给定数字的素数,以及所有这些素数乘积的自然对数。因此,因为我们正在使用对数,所以我也可以添加每个小于给定数字的素数的自然对数。所以我创建了这个程序,但我觉得对数有点奇怪?我尝试检查我的程序给出的总和是否正确,但首先我的程序只给出整数作为答案,其次我的计算器给出了不同的答案。我的代码有问题吗?素数工作正常,但如前所述,对数是错误的。我的代码见下文

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

【问题讨论】:

  • 这段代码是否正确缩进?
  • 我这么认为?当我运行我的程序时,它工作正常,除了日志的总和大声笑

标签: python loops sum primes logarithm


【解决方案1】:

虽然你的程序相当混乱(而且判断数是否为素数的方法远非最优,但可能有一个错误)。

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

+= 表示您使用操作数“增加”变量,因此您声明:

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

【讨论】:

  • 我修复了这个问题并让素数检查器更快,但是在运行这段代码时我仍然得到整数:(就像当我想要 8 以下素数的自然对数总和时,我得到 5 5.347 .... 和 n 之间的比率总和(因此 n/对数素数的总和)向下舍入为 1 而不是 1.4961 ......我试过使用 float 但它没有帮助:(
  • @pokemonfan:你确定这不是数字格式错误。我在终端中对其进行了测试(见答案)并得到了预期的结果......
  • 嗯,这听起来可能很愚蠢,但数字格式错误是什么?
  • 好吧,您使用%d,正如您在documentation 中看到的那样,这意味着您将数字打印为整数,通过使用%f,您将打印float 因此带有小数点等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
相关资源
最近更新 更多