Perfect Number Checker
检查数字是否为完美数、亏数或丰数,查找真因数和因数和。
查找完全数
已知完全数
关于此工具
完全数代表了一个罕见的数学现象,其中正整数等于其所有真因子之和——这个性质如此优雅,以至于古代数学家认为它们是神圣的。这些数字与数论、素因数分解和数学美的研究深深相连,远不仅仅是数学奇趣。完全数已经吸引数学家超过两千年,尽管经过几个世纪的搜索,仅发现51个已知例子,其神秘性至今继续。
使用此检查器来探索任何整数的分类:它是完全数、盈数(真因子和超过自身)还是亏数(真因子和不足)?该工具会立即计算所有真因子及其和,提供对数字结构的完整透明度。这对学习整除性质的学生、调查数论模式的数学家、从事梅森质数算法研究的研究者,或对数字中隐藏算术结构好奇的任何人都是无价的。
有趣的是,所有已知完全数都是偶数,并遵循欧拉优雅的公式:2^(p−1) × (2^p − 1),其中(2^p − 1)是梅森质数。寻找新的完全数等同于发现新的梅森质数——这是一个通过GIMPS等项目将娱乐数学与尖端计算研究相连接的探求。无论你是在验证猜想、教授整除性质,还是单纯地为数学结构而惊叹,这个工具都能将抽象数论转化为切实、可验证的结果。
常见问题
代码实现
def get_proper_divisors(n: int) -> list[int]:
"""Get all proper divisors of n (excluding n itself)."""
if n <= 1:
return []
divisors = [1]
i = 2
while i * i <= n:
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n // i)
i += 1
return sorted(divisors)
def check_perfect(n: int) -> dict:
"""Check if n is perfect, deficient, or abundant."""
if n <= 0:
return {"error": "Must be positive"}
divisors = get_proper_divisors(n)
divisor_sum = sum(divisors)
if divisor_sum == n:
classification = "perfect"
elif divisor_sum < n:
classification = "deficient"
else:
classification = "abundant"
return {
"number": n,
"divisors": divisors,
"divisor_sum": divisor_sum,
"is_perfect": divisor_sum == n,
"classification": classification,
"difference": n - divisor_sum
}
# Known perfect numbers
perfect_numbers = [6, 28, 496, 8128, 33550336]
for p in perfect_numbers:
r = check_perfect(p)
print(f"{p}: {r['classification']} (divisors sum = {r['divisor_sum']})")
print(f" Divisors: {r['divisors']}")
# Find abundant numbers up to 100
print("\nAbundant numbers up to 100:")
abundant = [n for n in range(2, 101) if check_perfect(n)["classification"] == "abundant"]
print(abundant)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.