简体   繁体   English

素数和元组

[英]Prime numbers and tuples

I am beginner in python and I am working on one problem which I am not able to solve.我是 python 的初学者,我正在解决一个我无法解决的问题。

What I am supposed to do: create a list of tuples with prime numbers, the range is two numbers(which are also prime numbers) and the tuples in that list are supposed to contain only 2 prime numbers p and p+2我应该做什么:创建一个带有素数的元组列表,范围是两个数字(也是素数),并且该列表中的元组应该只包含 2 个素数 p 和 p+2

For example : given range (11, 31) returned list = [(11, 13), (17, 19), (29, 31)]例如:给定范围 (11, 31) 返回列表 = [(11, 13), (17, 19), (29, 31)]

This is my code这是我的代码

def twin_primes(a: int, b:int) -> List[Tuple[int, int]]:

    list_primes = []
    list_final = []
    for val in range (a, b+1):
        if val > 1 :
            for n in range(2, val):
                if (val % n) == 0:
                    break
            else:
                list_primes.append(val)
    for val in list_primes:
        print(val)
        list_final.append((list_primes[val], list_primes[val + 2]))
    return list_final

print(twin_primes(11,31))

in the first for cycle I determine which numbers in that range are prime and append them to list_primes在第一个循环中,我确定该范围内的哪些数字是素数,append 它们到list_primes

in the second for cycle I tried to take the prime numbers from list_primes and append them as tuples into list_final在第二个循环中,我尝试将list_primes和 append 中的素数作为元组放入list_final

it tells me this:它告诉我:

list_final.append((list_primes[val], list_primes[val + 2])) list_final.append((list_primes[val], list_primes[val + 2]))
IndexError: list index out of range* IndexError:列表索引超出范围*

could someone please help me with this?有人可以帮我吗? I think I understand that error but I don't know how to fix the code so it just takes the p , p+2 into one tuple and then it would take another pair and so on... also it has to ignore 23 even though it is prime number.我想我理解这个错误,但我不知道如何修复代码,所以它只需要pp+2进入一个元组,然后它会需要另一对等等......它甚至必须忽略 23虽然它是质数。

Your logic for iterating over the twins is incorrect.您对双胞胎进行迭代的逻辑是不正确的。 val is set to the element in the list itself, not the index. val设置为列表本身中的元素,而不是索引。 You're treating it as if it were an index.您将其视为索引。 You need to iterate over a range of numbers, not the elements themselves.您需要迭代一系列数字,而不是元素本身。 I suggest using a range with a step of 2.我建议使用步长为 2 的范围。

list_primes = []
list_final = []
for val in range (a, b+1):
    if val > 1 :
        for n in range(2, val):
            if (val % n) == 0:
                break
        else:
            list_primes.append(val)

for i in range(0, len(list_primes) - 1, 2):
    list_final.append((list_primes[i], list_primes[i + 1]))
return list_final

This produces [(11, 13), (17, 19), (23, 29)] when a = 11 and b = 31 .当 a = 11和 b = 31时,这会产生[(11, 13), (17, 19), (23, 29)]

But your logic still requires some tweaking since this produces [(53, 59), (61, 67), (71, 73), (79, 83), (89, 97)] when a = 50 and b = 100 , and obviously this is not correct.但是您的逻辑仍然需要进行一些调整,因为当 a = 50和 b = 100时,这会产生[(53, 59), (61, 67), (71, 73), (79, 83), (89, 97)] ,显然这是不正确的。

val is not an index into list_primes , it's an element of it. val不是list_primes的索引,它是它的一个元素。 You need to append a tuple containing val and val+2 to list_final , but only if val+2 is in the list of primes.您需要 append 一个包含valval+2list_final的元组,但前提是val+2在素数列表中。

for val in list_primes:
    if val+2 in list_primes:
        list_final.append((val, val+2))

This can be improved using enumerate() , since it will always be the case that val+2 will be the next prime if it exists.这可以使用enumerate()来改进,因为如果val+2存在,它总是会成为下一个素数。

for i, val in enumerate(list_primes[:-1]):
    if list_primes[i+1] == val + 2:
        list_final.append((val, val+2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM