Python-100-Days icon indicating copy to clipboard operation
Python-100-Days copied to clipboard

a bug of code of Day05

Open ShTwc opened this issue 4 years ago • 2 comments

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).

ShTwc avatar Nov 09 '21 11:11 ShTwc

Thank you very much!

jackfrued avatar Nov 20 '21 11:11 jackfrued

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.

ggggggggggggg1 avatar Nov 26 '23 12:11 ggggggggggggg1