题解 | 完全数计算
完全数计算
https://www.nowcoder.com/practice/7299c12e6abb437c87ad3e712383ff84
def is_perfect(num): total = 0 for i in range(1, num + 1): if num % i == 0: total += i if total - num == num: return True else: return False # print(is_perfect(12)) # count all n = int(input()) num = 0 for i in range(1, n): if is_perfect(i): num += 1 # print(i) print(num)
以上方法就是硬做了,实际上也能过。示例1的第三个数496没给出,可以参考一下。
但是感觉其实不需要查询完整的range(1, num+1),查一半然后加也行,懒得写了。