亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何打印出兩個數字的公因數列表?

如何打印出兩個數字的公因數列表?

眼眸繁星 2022-05-19 18:49:39
我需要打印出兩個數字的公因數列表def print_nums(x, y):    for i in range(1, x + 1):        if x % i == 0:            print(i)    for t in range(1, y + 1):        if y % t == 0:            print(t)number = int(input("Enter a number: "))number2 = int(input("Enter a second number: "))print("Common factors are: ".format(number, number2))print_nums(number, number2)它打印出兩個列表,但不是每個列表的公因子
查看完整描述

3 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

def print_nums(x, y):

    zet = []

    for i in range(1, x + 1):

        if x % i == 0:

            #print(i)

            zet.append(i)

    for t in range(1, y + 1):

        if y % t == 0 and t in zet:

            print(t)



number = int(input("Enter a number: "))

number2 = int(input("Enter a second number: "))


print("Common factors are:")

print_nums(number, number2)

運行代碼示例:


Enter a number: 24                                                                                                                                                                  

Enter a second number: 18                                                                                                                                                           

Common factors are:                                                                                                                                                                 

1                                                                                                                                                                                   

2                                                                                                                                                                                   

3                                                                                                                                                                                   

6  


查看完整回答
反對 回復 2022-05-19
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

您的代碼只打印因子,而不是公因子。您可以遍歷xandy的公共范圍并檢查是否i是兩者的一個因素:


def common_factors(x, y):

    for i in range(2, min(x, y)+1):  # 1 is trivial, so ignore it

        if x % i == 0 and y % i == 0:  # If x and y are both multiples of i

            yield i


print(list(common_factors(9, 12)))  # -> [3]


查看完整回答
反對 回復 2022-05-19
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

與上述解決方案基本相同的想法..但是一個漂亮的函數基本上只檢查列表中非唯一的項目..就像 set 的對立面...這會返回共同因素,因為它們會出現不止一次


from iteration_utilities import duplicates, unique_everseen


facs=[]


def print_nums(x, y, facs):

    for i in range(1, x + 1):

        if x % i == 0:

            facs.append(i)

    for t in range(1, y + 1):

        if y % t == 0:

            facs.append(t)

    return list(unique_everseen(duplicates(facs)))


查看完整回答
反對 回復 2022-05-19
  • 3 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號