python - 计算素数对数的程序

标签 python math

数学规则表明我的“程序”给出了错误的答案。

如果您能检查一下这段代码并告诉我其中的问题,我将不胜感激。我知道问题出在 ll = [] 行之后的某处。我只是无法确定它的确切原因。但我确实知道 所有小于 n 的素数的对数之和小于 n。我的程序违反了这条规则。

代码如下:

from math import log
lp = [] ## create a list
for n in range(2,10000):
    for x in range(2,n):
        if n % x == 0:
            break
    else:
        lp.append(n) ## fill that list with the primes
##print lp[500] found the value of lp[500]
ll = [] ## create a second list
for i in range(2, lp[500]):
        if i < 3581: ## this is the number corresponding to lp[500]
            i = log(i, )
            ll.append(i) ## fill the second list with logs of primes
print sum (ll), 3581, sum(ll)/3581`

最佳答案

您的第二个列表不仅包含质数的对数,还包含 2 到 lp[500] 之间的所有整数的对数。

关于python - 计算素数对数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118606/

相关文章:

python - 如何将两个 csr_matrix 合并为一个?

c++ - 从 exprtk 编译 win32 库

Python:无法分配给运算符

algorithm - 生成天间隔的间隔重复算法是什么?

algorithm - 查找具有 n-1 个相等分量的向量

python - "0-th value returned by pyfunc_0 is double, but expects float"虽然我认为它返回 float

Java "Virtual Machine"与 Python "Interpreter"的说法?

用于 Windows 的 python whois

java - 迷失与弦

python - 如何让 for 循环在 python 2.7 中与 tkinter 一起工作?