Python 素数、对数、求和和和循环
我正试图制作一个程序来计算所有小于给定数的素数,以及所有这些素数乘积的自然对数。因为我们使用对数,我也可以把每个小于给定数的素数的自然对数相加。所以我创建了这个程序,但是我觉得对数有点奇怪?我试着检查我的程序给出的总和是否正确,但首先我的程序只给出整数作为答案,其次我的计算器给出不同的答案。我的代码有问题吗?素数工作正常,但如前所述,对数是错误的。请参阅下面的代码Python 素数、对数、求和和和循环,python,loops,sum,primes,logarithm,Python,Loops,Sum,Primes,Logarithm,我正试图制作一个程序来计算所有小于给定数的素数,以及所有这些素数乘积的自然对数。因为我们使用对数,我也可以把每个小于给定数的素数的自然对数相加。所以我创建了这个程序,但是我觉得对数有点奇怪?我试着检查我的程序给出的总和是否正确,但首先我的程序只给出整数作为答案,其次我的计算器给出不同的答案。我的代码有问题吗?素数工作正常,但如前所述,对数是错误的。请参阅下面的代码 def prime_program(): while True: answer = raw_input("Do you w
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()
虽然您的程序非常混乱(并且确定数字是否为素数的方法远远不是最优的,但可能有一个错误)
+=
表示使用操作数“增加”变量,因此可以声明:
log_of_prime_sum = log_of_prime_sum+log_of_prime_sum+float(log(2.0))
防止此类错误的一种方法是,例如,初始化(在程序顶部,log\u of_prime\u sum
已经使用了float(log(2.0))
,它甚至会稍微提高算法的性能
换言之,你将素数和的对数加倍
但我建议您使代码更具可读性,并使用更快的素数检查。例如,您可以停止在日志(n)中使用您希望检查的数字n枚举潜在的除法器,以便读取:
for x in range(2,int(sqrt(prime))) :
if prime%x == 0.0:
break
可能建议使用int
进行素数计算,如果希望将其添加到素数和的日志中,则仅将其转换为float
如果在终端中运行此程序:
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))
这段代码缩进正确吗?我想是的。当我运行我的程序时,它运行得很好,除了日志的总和,洛利修正了这一点,使质数检查器更快,但在运行这段代码时,我仍然得到整数:(比如当我想要素数的自然对数之和小于8时,我得到的是5,而不是5.347….而且和n之间的比率(因此n/对数素数之和)被舍入为1,而不是1.4961….我尝试过使用float,但没有任何帮助:(@pokemonfan:你确定这不是一个数字格式错误。我在终端上测试了它。)(参见答案)并得到了预期的结果…嗯,这听起来可能很愚蠢,但数字格式错误是什么?您可以使用%d
,正如您在中所看到的,这意味着您将数字打印为整数,通过使用%f
您将打印带有小数点等的浮点。
for x in range(2,int(sqrt(prime))) :
if prime%x == 0.0:
break
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