Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 素数列表-意外输出_Python_Python 3.x_Primes - Fatal编程技术网

Python 素数列表-意外输出

Python 素数列表-意外输出,python,python-3.x,primes,Python,Python 3.x,Primes,输出:而不是我试图获取的素数列表 import math #here i try to use trial division to validate whether a number is false or not def isPrime(n): d = {} u = math.floor(math.sqrt(n)) i = 2 while (i <= u): if (n % i == 0): return

输出:
而不是我试图获取的素数列表

import math
#here i try to use trial division to validate whether a number is false or not     
def isPrime(n):
    d = {}
    u = math.floor(math.sqrt(n))
    i = 2
    while (i <= u):
        if (n % i == 0):
            return False
        i +=1
        return True
#here I attempt to find all the prime numbers between 1 and 5000
print(isPrime(n) for n in range(1,5000))
导入数学
#在这里,我尝试使用试算法来验证一个数字是否为假
def iPrime(n):
d={}
u=数学地板(数学sqrt(n))
i=2

而(我我想你想做的是:

import math
def isPrime(n):
    u = math.floor(math.sqrt(n))
    i = 2
    while (i <= u):
        if (n % i == 0):
            return False
        i +=1
    return True

for n in range(1, 5000):
    print(isPrime(n))
导入数学
def iPrime(n):
u=数学地板(数学sqrt(n))
i=2

而(i您构建了一个生成器,然后告诉Python打印该对象。这就是您得到的。根据您的描述,我认为您需要一个列表理解,它将为您提供一个素数列表

试试这个:

print ( [n for n in range(1, 5000) if isPrime(n) ] )

请注意,您希望打印素数,而不是来自isPrime

的返回值。如果您使用
print(列表(isPrime(n)表示范围(15000)内的n))
,会发生什么情况?这是python代码吗?也许您应该添加适当的语言标记。