如何检查一个数字是否可以被列表中的每个数字整除

时间:2016-07-11 07:42:10

标签: python list solution

我对此完全无能为力,我发现的唯一解决方案是使用生成器(我不明白,因为我刚开始“尝试编码”)。

这是我的代码:

x = 9

PrimeCount = 4

PrimeList = [2, 3, 5, 7]

while PrimeCount < 10002:
    if all():
        y == x
        PrimeList.append(y)
        PrimeCount += 1

    x += 2
    y == x

print (x-2)
print (PrimeList)

我正在尝试做的是解决Project Euler问题。 7.(找到第10001个素数),所以我想查看所有奇数并检查它们是否可以被我之前检查过的其他数字整除。

我知道,这是一个非常低效的解决方案,但它是我自己想到的唯一一个。

提前致谢!

6 个答案:

答案 0 :(得分:1)

  

如何检查数字是否可以被列表中的每个数字整除

>>> def divisible(n, lst):
...     return all(map(lambda y: n%y == 0, lst))
...     
>>> divisible(10, [2,5])
True
>>> divisible(10, [2,5, 7])
False

只需检查列表中每个元素的数字模数是否等于0

答案 1 :(得分:0)

这也是一种蛮力解决方案,类似于您正在尝试且更容易理解的解决方案:

primeList = [2]
check = 3

while len(primeList)<6:
    flag = True
    for num in primeList:
        if check%num == 0:
           flag = False
    if flag:
        primeList.append(check)

    check += 2



print primeList[-1]  

primeList:将包含所有素数。它应该用第一个初始化(你可以用更多来初始化它,但是你应该更改检查)

primeCount:使用列表的长度代替,更容易 - &gt; LEN(primeList)。

检查:是要检查的号码(你的x)。您应该从下一个奇数开始(如果您在primeList中的最后一个数字为2,则检查为3,依此类推)

在第一次检查时,长度小于您要查找的第n个数字。在for循环中,检查数字是否为素数,以及更新标志和列表以防万一。然后转到下一个奇数。

最后,打印主要列表的最后一个元素。

要知道第10001个素数, while len(primeList)<10001:

答案 2 :(得分:0)

可以将生成器视为返回列表的函数/子例程,但一次只能执行一个元素。因此,您可以单独与每个元素进行交互,而无需将整个列表存储在内存中并循环遍历它。

素数生成器,理想情况下,将返回所有素数列表中的下一个素数。很多函数谷歌只是向我扔了一个范围内的素数列表或最多值。

我无法快速找到素数。发生器很短(粘贴),所以让我们假设我使用了Eli Bendersky在Q567222 simple-prime-generator-in-python中描述的Erastothenes筛子。他定义了一个素数。生成器能够生成一个名为gen_primes()的无限列表,但任何生成器都不必提供范围,或者它试图在其中找到素数的值的上限。

所以你的代码可能会成为:

#<insert def of gen_primes here>
int PrimeCount = 0;

PrimeList = [];

while PrimeCount < 10002:
    prime_no = gen_primes()
    PrimeCount += 1
    PrimeList.append(prime_no)

print (prime_no)
print (PrimeList)

PrimeList包含10001个条目并且生成器返回的最后一个素数是您正在寻找的那个时,循环将退出。 虽然我也会质疑然后使用10001条目打印列表而没有任何格式化的有用性。

答案 3 :(得分:0)

  

如何检查数字是否可以被列表中的每个数字整除

你真的是指每个,当然要找到一个素数,你需要知道这个数字是否可以被列表中的任何数字整除。

可能会有更整洁,更快或更优雅的解决方案,我试图将这个解决方案与您自己的代码放在一起,并使其尽可能易于理解。但是,我定义的函数会检查数字是否可以被列表中的任何数字整除,因为这是查找素数所需的数字。一旦找到可以除以试验编号的编号,它也不会检查列表中的任何其他编号。 HTH:

x = 9
# PrimeTarget is the nth prime number - the one being searched for.
PrimeTarget = 10001

PrimeList = [2, 3, 5, 7]

def check_prime(guess, primes):
# Routine to check if guess is divisible by any number in primes.
    for prime_no in primes:
        if guess % prime_no == 0:
            # guess is divisible by a prime no.
            # and thus not prime itself.
            return False
    # Only hit the next line if the guess was not divisible
    # by any of the numbers in the list (primes)
    return True

while len(PrimeList) < PrimeTarget:
    if check_prime(x, PrimeList):
        PrimeList.append(x)

    x += 2

print (x)
print (PrimeList)

一个强大的整洁功能将是:

def check_prime(guess, primes):
    return any( guess % x == 0 for x in primes)

如果您确实想检查某个数字是否可以被列表中的所有数字整除,请将any更改为all

答案 4 :(得分:0)

您可以尝试使用此python代码查找给定范围内的所有素数。通过一些修改,您还可以通过检查列表中是否存在来检查给定的是否为素数。

## method for determining the no of prime nos in a given set of integers

till= int(input('the no upto which u want to find the prime numbers : '))
list=[2,3]
for n in range(4,till):
    test=[]
    for l in list:
        a=n%l
        if a!=0:
            test.append(1)
    if sum(test)==len(list):
        list.append(n)
print(list)

答案 5 :(得分:-1)

我将用C ++编写代码,但我确信您能够理解并实现它:

&#13;
&#13;
int x = 9;
int PrimeCount = 4;
bool isPrime;
PrimeList = [2, 3, 5, 7];

While(PrimeCount < 1002) 
{
  for (int i = 0; i < PrimeCount; i++) 
  {
    //Check if multiple of any of the primes in the list
    if (x % PrimeList[i] == o)
      isPrime = false;
    else
      isPrime = true;
  }
  //If not multiple of available primes, add to array and increment PrimeCount
  if (isPrime) 
  {
    PrimeList[PrimeCount] = x;
    PrimeCount++;
  }
  //Increment x and repeat
  x++;
}
&#13;
&#13;
&#13;

我只给你逻辑,因为我不确定你是否可以像我那样添加数组中的元素。希望这会有所帮助。

相关问题