python:友好数字检查

标签 python python-3.x

我需要用 Python 编写一个函数来告诉我两个数字 (m,n) 是否友好。 我知道友好数是两个不同的数,它们的相关性如此之高,以至于每个数的真除数之和等于另一个数。

我无法从这里获得通行证

def ami_check(m,n):

我将不胜感激。

最佳答案

定义,来自 WikipediaAmicable 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.

分解一下:

  1. 两个不同的数字 OK
  2. 每个真除数的和 OK
  3. 等于另一个数 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/

相关文章:

python-3.x - 通过 Webhooks 和 PubSubHubbub (Python3) 的 YouTube 推送通知在订阅时返回 200,但在视频上传时没有返回任何内容

python - Python 是否对列表的副本使用写时复制?

python - Fabric put 命令给出 fatal error : 'No such file' exception

python - 升级到python 3.0的技巧?

python-3.x - Py4JJavaError : An error occurred while calling o26. Parquet 。 (阅读 Parquet 文件)

python - 从 Json 字符串创建 Python 对象

python - 在 Selenium WebDriver 中使用 "Python"获取此消息 : object has no attribute

python - 在 Python 中访问嵌套的 JSON 元素

python - 将 Rasa 聊天机器人连接到 Webchat

python - OpenCV:识别图像的部分