47.3k views
0 votes
A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers less than 10,000. Write a program to find all these four numbers.

User NetCito
by
8.5k points

1 Answer

2 votes

i = 1

while i < 10001:

total = 0

x = 1

while x < i:

if i % x == 0:

total += x

x+=1

if total == i:

print(str(i)+" is a perfect number")

i += 1

When you run this code, you'll see that 6, 28, 496, and 8128 are all perfect numbers.

User ArchNoob
by
8.2k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.