如何使用Python检查两个数字(m,n)是否友好?
和睦号码是两个不同的号码,因此彼此相关,以使每个号码的适当除数之和等于另一个号码。(数字的适当除数是该数字的正数,而不是数字本身。例如,数字6的适当除数是1、2和3。)
在python中,您可以通过将每个数字的总和与另一个进行比较来找到这些数字。例如,
def are_amicable(x, y)
if x==y:
return False
# Find sum of their proper divisors
sum_x = sum(e for e in range(1, x//2+1) if x % e == 0)
sum_y = sum(e for e in range(1, y//2+1) if y % e == 0)
#Return true of they satisfy the last condition
return sum_x==y and sum_y==x
print(are_amicable(15, 20))
print(are_amicable(220, 284))这将给出输出
False True
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短