Python 基础知识简单

友好的一对

“友好配对”问题的详细指南和 Python 实现。

问题陈述

简单

编写一个函数 is_friendly_pair(a, b) ,它接受两个正整数 ab ,如果它们形成友好的对,则返回 True ,否则返回 False 。如果两个数字具有相同的丰度指数,则它们构成友好对。数字 n 的丰度指数定义为 sigma(n) / n,其中 sigma(n)n 的所有除数之和(包括 n 本身)。如果 sigma(a) / a == sigma(b) / b ,则两个数字是友好的。为了避免浮点问题,请通过交叉相乘进行比较:sigma(a) * b == sigma(b) * a

约束条件
  • 1 <= a, b <= 10^5

示例

Example 1
Input
a = 6, b = 28
Output
True
Explanation

sigma(6) = 1+2+3+6 = 12. sigma(28) = 1+2+4+7+14+28 = 56. Cross check: 12 * 28 = 336, 56 * 6 = 336. They are equal, so they are a friendly pair.

Example 2
Input
a = 30, b = 140
Output
True
Explanation

sigma(30) = 72, sigma(140) = 336. Cross check: 72 * 140 = 10080, 336 * 30 = 10080. Equal, so friendly pair.

Example 3
Input
a = 5, b = 10
Output
False
Explanation

sigma(5) = 6, sigma(10) = 18. Cross check: 6 * 10 = 60, 18 * 5 = 90. Not equal, so not a friendly pair.

Need a Hint?
考虑使用特定于基础的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。