如何在使用 return 函数时让 python 中定义的函数循环?

How do I get a defined function in python to loop whilst using the return function?

I have written a python code to generate a list of all Mersenne primes q for p less than n_max. This function works when using print, however I need to use return but this stops the loop and only outputs the first mersenne prime 3.

def primes(n):
    i, p, ps, m = 0, 3, [2], n // 2
    sieve = [True] * m
    while p <= n:
        if sieve[i]:
            ps.append(p)
            for j in range((p*p-3)//2, m, p):
                sieve[j] = False
        i, p = i+1, p+2
    return ps

def lucas_lehmer(p):
    if p == 2: return True
    m, i, s = pow(2,p) - 1, 3, 4
    while i <= p:
        i, s = i+1, (pow(s,2) - 2) % m
    return s == 0

def mersenne_prime(n_max):
    L1 = []
    for p in primes(n_max):
        if lucas_lehmer(p):
            q=(2**p)-1
            L1.append(q)
            return L1

谁能给出一个解决方案,使得代码 returns 所有 q=2**p-1 中 p 小于 n_max 的梅森素数?

你需要初始化一个列表a来存储所有的素数。然后将 q 添加到它。

def mersenne_prime(n_max):
    a=[]
    for p in primes(n_max):
        print(p)
        if lucas_lehmer2(p):
            q=(2**p)-1
            a.append(a)
    return a