![](/img/trans.png)
[英]How can I check if a number is divisible by one of the elements in a list (Python)?
[英]How to check if a number is divisible by every number in a list?
我對此完全無能為力,我找到的唯一解決方案是使用生成器(我不明白,因為我剛剛開始“嘗試編碼”)。
這是我的代碼:
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 個素數),所以我想遍歷所有奇數並檢查它們是否可以被我之前檢查過的所有其他數字整除。
我知道,這是一個非常低效的解決方案,但它是我自己想出的唯一解決方案。
如何檢查一個數字是否可以被列表中的每個數字整除
>>> 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
這也是一種蠻力解決方案,類似於您正在嘗試並且更容易理解的解決方案:
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:改用列表的長度,更容易-> len(primeList)。
檢查:是要檢查的數字(您的 x)。 您應該從下一個奇數開始(如果您在 primeList 中的最后一個數字為 2,則檢查將為 3,依此類推)
在第一個 while 檢查長度是否小於您要查找的第 n 個數字。 在 for 循環中,檢查數字是否為素數,並更新標志和列表以防萬一。 然后轉到下一個奇數。
最后,打印素數列表的最后一個元素。
要知道第 10001 個素數, while len(primeList)<10001:
生成器可以被認為是一個函數/子程序,它返回一個列表,但一次只返回一個元素。 因此,您可以單獨與每個元素進行交互,而不必將整個列表存儲在內存中並循環遍歷它。
理想情況下,素數生成器將返回所有素數列表中的下一個素數。 谷歌剛剛向我拋出的許多函數都會返回一個范圍內或達到某個最大值的素數列表。
我無法很快找到一個質數。 生成器很短(粘貼),所以讓我們假設我正在使用 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 個條目而沒有任何格式的列表的用處。
如何檢查一個數字是否可以被列表中的每個數字整除
你真的是說每個,一定要找到一個質數,你需要知道這個數字是否可以被列表中的任何數字整除。
將會有更簡潔、更快或更優雅的解決方案可用,我已經嘗試將您自己的代碼放在一起,並使其盡可能易於理解。 但是,我定義的函數會檢查一個數字是否可以被列表中的任何數字整除,因為這是查找素數所必需的。 一旦它找到了可以除以試用號的數字,它也不會檢查列表中的任何其他數字。 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
您可以嘗試使用此 python 代碼來查找給定范圍內的所有質數。 通過一些修改,您還可以通過檢查它在列表中的存在來檢查給定的 no 是否是素數。
## 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)
我將用 C++ 編寫代碼,但我相信您能夠理解並實現它:
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++; }
我只是給你邏輯,因為我不確定你是否可以像我那樣在數組中添加元素。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.