永发信息网

求python高手。求素数的乘机。题目如图

答案:4  悬赏:50  手机版
解决时间 2021-04-02 21:23
  • 提问者网友:戎马万世
  • 2021-04-02 01:07
求python高手。求素数的乘机。题目如图
最佳答案
  • 五星知识达人网友:舊物识亽
  • 2021-04-02 02:38
程序如下,因为里面用来range,所以如果输入的数字太大的话会死掉。最好不要超过10000,
另外,你对英文原题的理解不对,不是第n个素数,而是素数n,
在__main__部分返回了小于n的所有素数列表,按照题目要求,打出了所有素数的log(n)之和,并除以n,验证随着n的增加,所得结果趋近于1.

'''
Created on 2011-9-19

@author: legendxx
'''
import math

def isPrime(x):
i=1
for i in range(int(math.sqrt(x)),0,-1):
if x%i==0:
break
if i==1:
return True
else:
return False

def GetPrimeList(x):
l = range(2,x)
i=0
while(iif isPrime(l[i]):
for j in range(2, l[-1] / l[i]):
try:
l.remove(j*l[i])
except ValueError:
pass
i+=1
else:
l.remove(l[i])

return l

if __name__ == '__main__':
n=int(input("input the number n:"))

l=GetPrimeList(n)

sum=0
for x in l:
sum+=math.log(x)
print sum,l[-1],sum/l[-1]
追问:题目是这样的:可能我理解错了,
There is a cute result from number theory that states that for sufficiently large n the product of the primes less than n is less than or equal to e**n and that as n grows, this becomes a tight bound (that is, the ratio of the product of the primes to e**n gets close to 1 as n grows)
追答:哦,那这样的话,n不一定要求是素数,最后一句话应该修改一下:
修改前: print sum,l[-1],sum/l[-1]
修改后:print sum,n,sum/n
打印素数乘积除以n,而不是除以最后一个素数。
全部回答
  • 1楼网友:北方的南先生
  • 2021-04-02 05:40
a1*a2*...an=e**n
其中a1,a2 是素数
其中e的值是多少?
追问:数学上不是有一个e。
loge =1:还有题目可能我理解错了,我发下吧:There is a cute result from number theory that states that for sufficiently large n the product of the primes less than n is less than or equal to e**n and that as n grows, this becomes a tight bound (that is, the ratio of the product of the primes to e**n gets close to 1 as n grows)
  • 2楼网友:人類模型
  • 2021-04-02 04:14
你这里运行时间是和N的大小密切相关的。你这个2秒是多大时候的N对应的时间呢?
这里给出一个还算可以的列素数的方法(后面的计算我是抄legendxx的,特此声明):
def primes(x):
if x==1:
return []
elif x==2:
return [2]
elif x==3:
return [2,3]
else:
a=primes(int(x/2))
b=list(range(int(x/2)+1,x+1))
for i in a:
for j in range(len(b)-1,-1,-1):
if b[j]%i==0:
del b[j]
return a+b
import math
if __name__ == '__main__':
n=int(input("input the number n:"))
l=primes(n)
sum=0
for x in l:
sum+=math.log(x)
print n,sum/n
  • 3楼网友:夜余生
  • 2021-04-02 03:13
分太少,提个分 可以考虑下优化问题。
不过可以断定 你的这个等式不成立是肯定的,话说整数的乘积还是整数,怎么可能和e的n次方相同,除了e^0=1,其他都是小数
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯