使用列表理解查找素数

Find prime numbers with list comprehension

我试图为列表中的素数 select 编写代码。用户给出一个限制,程序显示从 2 到限制的所有质数。我试图尽可能减少最大行数,但对某些我无法理解的情况感到惊讶。如果你能帮助我,我将不胜感激。

我写了这段代码:

# returns all integers from 2 to a limit given by the user. 

def primes(limit):
    # generates the numbers. 
    lista = range(2, limit + 1)
    
    p = 2
    
    while p < limit:
        #filters the prime numbers and places in a list.
        lista = [i for i in lista if i == p or i % p != 0]
    
        p += 1
    

    return lista

        
def main():
    #asks the user for the limit number.
    l = int(input("Enter the limit: "))
    
    #call the function which selects the numbers and returns the result. 
    return print(primes(l))

#Ensures that the main program only runs when the functions have not been imported into another file.
if __name__ == '__main__':
    main()

它按预期运行,但是当我尝试删除第一个列表赋值行并将范围函数直接包含到理解中时,它不起作用。为什么?

# returns all integers from 2 to a limit given by the user. 

def primes(limit):
    p = 2
    
    while p < limit:
        #filters the prime numbers and places in a list.
        lista = [i for i in range(2, limit + 1) if i == p or i % p != 0]
    #or lista = [i for i in range(2, limit + 1) if i == p or i % p != 0]
    #or lista = [i for i in [*range(2, limit + 1)] if i == p or i % p != 0]
    
        p += 1
    

    return lista

        
def main():
    #asks the user for the limit number.
    l = int(input("Enter the limit: "))
    
    #call the function which selects the numbers and returns the result. 
    return print(primes(l))

#Ensures that the main program only runs when the functions have not been imported into another file.
if __name__ == '__main__':
    main()

其他问题。由于带有 range 的行不是列表,所以我修复它只是为了改进代码,但是当我将值的名称从 'lista' 更改为另一个名称时,我发现它也不起作用。为什么?

# returns all integers from 2 to a limit given by the user. 

def primes(limit):
    # generates the numbers. 
    nums = range(2, limit + 1)

    p = 2
    
    while p < limit:
        #filters the prime numbers and places in a list.
        lista = [i for i in nums if i == p or i % p != 0]
    
        p += 1
    

    return lista

        
def main():
    #asks the user for the limit number.
    l = int(input("Enter the limit: "))
    
    #call the function which selects the numbers and returns the result. 
    return print(primes(l))

#ensures that the main program only runs when the functions have not been imported into another file.
if __name__ == '__main__':
    main()

感谢您的关注。

这条单线效果很好:

def primes(val):
    return [x for x in range(2, val) if all(x % y != 0 for y in range(2, x))]

print(primes(10))

感谢您 attention.I 喜欢我们朋友 Yash Makan 的回答,但是当我尝试更大的数字时,比如 100000,它从来没有让我得到结果(或者我没有那么耐心等待) .所以我继续思考这个问题并得到以下结果,这是我可以通过列表理解实现的计算这个问题的最快方法。请注意您计算数百万个数字的速度有多快。

# returns all integers from 2 to a limit given by the user. 

def primes(limit):
    
    l = [i for i in range(2, limit + 1) if all(i % j != 0 for j in [2, 3, 5])]    
    lista = []
    return [2, 3, 5] + [lista.append(i) or i for i in l if all( i % j != 0 for j in lista[:int((len(lista) ** .5) + 1)])]

        
def main():
    l = int(input("Enter the limit: "))
    
    return print(primes(l))

if __name__ == '__main__':
    main()