如何使用 Python 查詢數字的因子?


為了查詢數字的因子,我們必須對從 1 到數字本身的所有數字執行一個 迴圈,並檢視該數字是否可以被整除。

示例

num=int(input("enter a number"))
factors=[]
for i in range(1,num+1):
    if num%i==0:
       factors.append(i)

print ("Factors of {} = {}".format(num,factors))

如果 i 能完全整除 num,那麼它就會被新增到 列表 中。最後,該列表將顯示為給定數字的因子。

輸出

enter a number75
Factors of 75 = [3, 5, 15, 25, 75]

更新於:2023 年 9 月 9 日

11K+ 瀏覽

啟動你的 職業

完成課程獲得認證

開始吧
廣告
© . All rights reserved.