新增: perfect_new.py,解决了perfect.py会把1当作完美数输出的问题

pull/792/head
Griffin-MX 2021-06-23 11:53:32 +08:00 committed by GitHub
parent 1ef5bf058a
commit 2ccf7a2120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,20 @@
"""
找出1~9999之间的所有完美数
完美数是除自身外其他所有因子的和正好等于这个数本身的数
例如: 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14
Differ: "1" 不是完美数
Version: 0.2
Author: Griffin Chen
Date: 2021-6-23
"""
import math
for num in range(2, 10000):
result = 0
for factor in range(1, int(math.sqrt(num)) + 1):
if num % factor == 0:
result += factor
if 1 < factor != num // factor:
result += num // factor
if result == num:
print(num)