a bug of code of Day05
run this code will get "prefect number" in range(10000), and, 1 is not a prefect number but show in result.
should change the range of "num" from range(1, 10000) to range(2, 10000).
Thank you very much!
ertainly! It seems like you want to find the "perfect numbers" within a certain range, and you've noticed that the number 1 is incorrectly being identified as a perfect number. By changing the range of "num" from range(1, 10000) to range(2, 10000), we can exclude the number 1 from the search for perfect numbers.
Here's the corrected code:
python def get_divisors_sum(num): divisors_sum = 0 for i in range(1, num): if num % i == 0: divisors_sum += i return divisors_sum
def find_perfect_numbers(): perfect_numbers = [] for num in range(2, 10000): # Change the range from (1, 10000) to (2, 10000) if get_divisors_sum(num) == num: perfect_numbers.append(num) return perfect_numbers
result = find_perfect_numbers() print(result) By making this change, the code will now correctly exclude the number 1 from the search for perfect numbers.