I was doing labs on perfect numbers in python, it worked fine and prints the numbers I need. But not sure if I need to put (1, 1000) in a range or (2, n + 1) ok? My commission is to ask me
"Write a python program to find all the ideal numbers from 1 to 10,000. When the ideal number is found, your logic should print it."
What is the ideal number:
In number theory, an ideal number is a positive integer equal to the sum of its own positive divisors, i.e. the sum of its positive divisors, excluding the number itself (also known as its sum of aliquots). Equivalently, a perfect number is a number that is half the sum of all its positive divisors (including itself), i.e. Σ1 (n) = 2n.
When I run my program, it gives 6, 28, 496 and 8128.
n = 1
while True:
factors = [1]
[factors.append(i) for i in range(2,n+1) if n%i == 0]
if sum(factors) == 2*n: print n
n += 1
source
share