阅读量:0
完数是指一个正整数的所有因子(包括1,不包括自身)的和等于这个数本身。下面是一个Python程序,用来找出一千以内的完数:
def find_perfect_numbers(limit): perfect_numbers = [] for num in range(1, limit): factors = [] for i in range(1, num): if num % i == 0: factors.append(i) if sum(factors) == num: perfect_numbers.append(num) return perfect_numbers limit = 1000 perfect_numbers = find_perfect_numbers(limit) print("Perfect numbers within 1000:") for num in perfect_numbers: print(num)
运行这个程序,就可以找到一千以内的完数。