簡體   English   中英

計數除數以檢測python 3.7.4中的素數

[英]Counting divisors to detect prime number in python 3.7.4

我試圖編寫一個代碼來自己檢測素數,但意外地,我發現它工作得很好:

def prime_number_check(inp):
    setp = [ ]
    res = [ ]
    count = 0
    for x in range(1, inp):
        setp.append(x)
    print(setp) #just to control the set
    for y in setp: #set of dividers
        if inp % setp[y-1] == 0:
            res.append(setp[y-1])
        else:
            continue
    print(res) #just to control the set
    for z in res:
        count += 1
    if count > 1:
        print(inp, "is not a prime number!")
    else:
        print(inp, "is a prime number!")

我試圖將輸入數字(將檢查它是否為素數)除以小於自身的數字,並想檢測結果集是否有浮點結果或整數結果,然后使用res.remove() ,那么如果有不是 1 的 int 結果,則打印not a prime ,否則打印prime ,但我失敗了; 而是找到了這個計算除數的代碼。

您能幫我找出如何在列表中進行 float/int 檢查並返回結果嗎? 我試過bool()isinstance()但總是有關於可迭代性的錯誤。

def prime(*arg):
    if num<2:
      return 0
    else:
      t=0
      for x in range(2,num+1):
         k=0
         for y in range(1,x+1):
           if x%y == 0:
             k += 1
         if k == 2:
           t += 1
      return t

在這里,我實際上沒有將 0 和 1 視為 prime 。 我希望你在 python 中得到了我的簡單代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM