打印列表中的行和列最少

时间:2019-02-28 04:44:56

标签: python function primes

查找素数的代码:

def findPrimes(n):

    prime_list = list()
    for number in range(1, n + 1):
        prime = True
        for i in range(2, number):
            if(number % i == 0):
                prime = False
        if prime:
            prime_list.append(number)

    return prime_list

整齐地列出素数的代码:

def displayPrimes(number, rows = 50):

    table_list = [[] for _ in range(rows)]
    primes = findPrimes(number)

    for index, item in enumerate(primes):
        row_index = index % rows
        table_list[row_index].append("%6d" % item)

    table_str = "\n".join(["\t".join(i) for i in table_list])

    return table_str
print(displayPrimes(4027))

寻找双素数的代码:

def findTwinPrimes(n):
     prime = [True for i in range(n + 2)] 
     p = 2

     while (p * p <= n + 1): 

        # If prime[p] is not changed,  
        # then it is a prime 
        if (prime[p] == True): 

            # Update all multiples of p 
            for i in range(p * 2, n + 2, p): 
                prime[i] = False
        p += 1

    # check twin prime numbers 
    # display the twin prime numbers 
        for p in range(2, n-1): 
            if prime[p] and prime[p + 2]: 
                print("(",p,",", (p + 2), ")" ,end='')

print(findTwinPrimes(4027))  

使双素数显示整齐的代码。 问题是它说:

  

TypeError:“函数”对象不可迭代

这行不通,因为我在代码中使用了相同的数据来显示素数,它只是整齐地显示了列表?

def displayTwinPrimes(number, rows = 52):

    table_list = [[] for _ in range(rows)]
    twinPrimes = findTwinPrimes

    for index, item in enumerate(twinPrimes):
        row_index = index % rows
        table_list[row_index].append("%6d" % item)

    table_str = "\n".join(["\t".join(i) for i in table_list])    

    return table_str

print(displayTwinPrimes(4027)) 

2 个答案:

答案 0 :(得分:0)

twinPrimes = findTwinPrimes

引用到findTwinPrimes函数,但不调用它,因为末尾没有括号。

如果要调用该函数,则需要括号:

twinPrimes = findTwinPrimes(some_argument_here)

答案 1 :(得分:0)

您需要重构代码以查找双胞胎号码,如下所示。

此方法返回双素数元组列表,即[(3,5),(5,7), ...]

def findTwinPrimes(n):
    prime = [True for i in range(n + 2)]
    p = 2
    twin_primes = []
    while (p * p <= n + 1):

        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):

            # Update all multiples of p
            for i in range(p * 2, n + 2, p):
                prime[i] = False
        p += 1

    # check twin prime numbers
    # display the twin prime numbers
    for p in range(2, n - 1):
        if prime[p] and prime[p + 2]:
            twin_primes.append((p, (p + 2)))
            print("(", p, ",", (p + 2), ")", end='')

    return twin_primes  # this returns twin_primes as list of tuples i.e [(3,5),(5,7), ...]

此外,您还需要按以下方式重新整理displayTwinPrimes方法:

def displayTwinPrimes(number, rows = 52):

    table_list = [[] for _ in range(rows)]
    twinPrimes = findTwinPrimes(number)

    for index, item in enumerate(twinPrimes):
        row_index = index % rows
        table_list[row_index].append(item)

    table_str = "\n".join(["\t".join(["(%6d, %6d)" % (p[0],p[1]) for p in i]) for i in table_list])

    return table_str