我需要用 Python 编写一个函数来告诉我两个数字 (m,n) 是否友好。 我知道友好数是两个不同的数,它们的相关性如此之高,以至于每个数的真除数之和等于另一个数。
我无法从这里获得通行证
def ami_check(m,n):
我将不胜感激。
最佳答案
定义,来自 Wikipedia 的 Amicable Numbers
:
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself.
分解一下:
两个不同的数字
OK每个真除数的和
OK等于另一个数
OK
然后:
def ami_check(x,y):
if x==y: return False # 1
sum_x=sum(e for e in range(1, x//2+1) if x%e==0) # 2
sum_y=sum(e for e in range(1, y//2+1) if y%e==0) # 2
return sum_x==y and sum_y==x # 3
关于python:友好数字检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317927/