Found a problem
MRAB
python at mrabarnett.plus.com
Tue Mar 9 18:19:20 EST 2021
On 2021-03-09 20:16, Victor Dib wrote:
> Olá, comunidade do Python!
>
[snip]
>
> def num_perf_inf(n):
> divisors = []
> perfects = []
> limit = n - 1
> for i in range(1, limit):
> dividend = i + 1
> for j in range(i):
> divisor = j + 1
> if dividend % divisor == 0:
> divisors.append(divisor)
> print(f'Divisors of {i + 1}: {divisors}')
>
> divisors.pop()
> if sum(divisors) == dividend:
> perfects.append(i)
> divisors.clear()
> print(perfects)
>
>
> num_perf_inf(28)
>
> E o resultado da execução desse código, em Python 3.9.2 e em Python 3.8.7 (como já mencionado, testei as duas versões da linguagem:
>
>
> Divisors of 2: [1]
> Divisors of 3: [1]
> Divisors of 4: [1, 2]
> Divisors of 5: [1]
> Divisors of 6: [1, 2, 3]
> Divisors of 7: [1]
> Divisors of 8: [1, 2, 4]
> Divisors of 9: [1, 3]
> Divisors of 10: [1, 2, 5]
> Divisors of 11: [1]
> Divisors of 12: [1, 2, 3, 4, 6]
> Divisors of 13: [1]
> Divisors of 14: [1, 2, 7]
> Divisors of 15: [1, 3, 5]
> Divisors of 16: [1, 2, 4, 8]
> Divisors of 17: [1]
> Divisors of 18: [1, 2, 3, 6, 9]
> Divisors of 19: [1]
> Divisors of 20: [1, 2, 4, 5, 10]
> Divisors of 21: [1, 3, 7]
> Divisors of 22: [1, 2, 11]
> Divisors of 23: [1]
> Divisors of 24: [1, 2, 3, 4, 6, 8, 12]
> Divisors of 25: [1, 5]
> Divisors of 26: [1, 2, 13]
> Divisors of 27: [1, 3, 9]
> [23]
>
You're omitting one of the factors by popping it from the list.
You're also appending 'i' to the list of perfect numbers instead of 'i +
1' (the dividend).
More information about the Python-list
mailing list