完數是指一個正整數的所有因子(包括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)
運行這個程序,就可以找到一千以內的完數。