How to find out perfect numbers with python

What is perfect Number?
Perfect number is a positive integer number in which sum of divisors(proper divisors) is equal to itself

Proper divisor the divisors of 10 is 1,2,5,10

Here 1,2,5 is proper divisor .. but 10 is not proper. because its the number we are divisioning

There is a perfect number in 10 is 6

Because the divisors of 6 is 1,2,3

The sum of the divisors= 1+2+3=6

So its a perfect number…

Now find out perfect numbers with python..

To find of perfect numbers first we need to get the divisors of that number

Find out the divisors of a number

Find out divisor of a number is easy with python.. we will use modulo % here

For example we can find divisor by checking is there any remaining after division ..

if we check 9%2 the remainder will be 1 .

Its like 9/2 2+2+2+2+1

2 goes 4 times easily in the division of 9 and remain 1

So 2 is not a divisor of 9

This example code prints the divisors of 100


number=100
for i in range(number):
	if number%(i+1)==0:
		print(i+1)
		#print(i+1,end=' ')


the result
1
2
4
5
10
20
25
50
100

Store the sum of the proper divisors


number=100
sum_of_the_proper_divisors=0
for i in range(number):
	if number%(i+1)==0:
		if i+1!=number:
			sum_of_the_proper_divisors+=i+1
		print(i+1)
		#print(i+1,end=' ')

print('sum_of_the_proper_divisors', sum_of_the_proper_divisors)


result
1
2
4
5
10
20
25
50
100
sum_of_the_proper_divisors 117

Next check out if the sum of the divisors of that number is equal to the number


number=100
sum_of_the_proper_divisors=0
for i in range(number):
	if number%(i+1)==0:
		if i+1!=number:
			sum_of_the_proper_divisors+=i+1
		#print(i+1,end=' ')

if number==sum_of_the_proper_divisors:
	print(number, "is a perfect number")
else:
	print(number, "is not a perfect number")


result
100 is not a perfect number

We can also make it wonderful by taking user input


number=int(input("Enter a positive number to check if its perfect number or not: "))
sum_of_the_proper_divisors=0
for i in range(number):
	if number%(i+1)==0:
		if i+1!=number:
			sum_of_the_proper_divisors+=i+1
		#print(i+1,end=' ')

if number==sum_of_the_proper_divisors:
	print(number, "is a perfect number")
else:
	print(number, "is not a perfect number")


result
Enter a positive number to check if its perfect number or not: 6
6 is a perfect number

What about get all the perfect number in a range


limit_number=1000
for number in range(1,limit_number+1):
	sum_of_the_proper_divisors=0
	for i in range(number):
		if number%(i+1)==0:
			if i+1!=number:
				sum_of_the_proper_divisors+=i+1
			#print(i+1,end=' ')

	if number==sum_of_the_proper_divisors:
		print(number)


result
6
28
496

We can optimize the divisors calculation for larger numbers with sqrt


import math
number=28
sum_of_the_proper_divisors = 0
for i in range(1, int(math.sqrt(number)) + 1):
	if number % i == 0:
		if i != number and i != 1:
			sum_of_the_proper_divisors += i
		# Add the corresponding divisor greater than sqrt(number)
		corresponding_divisor = number // i
		if corresponding_divisor != number and corresponding_divisor != i:
			sum_of_the_proper_divisors += corresponding_divisor
# 1 is always a proper divisor (except for 1 itself, which isn't a proper divisor)
if number != 1:
	sum_of_the_proper_divisors += 1


if number==sum_of_the_proper_divisors:
	print(number, "is a perfect number")
else:
	print(number, "is not a perfect number")


result
28 is a perfect number

Calculate with range with the optimized version


import math

limit_number = 10000
for number in range(1, limit_number + 1):
    sum_of_the_proper_divisors = 0
    for i in range(1, int(math.sqrt(number)) + 1):
        if number % i == 0:
            if i != number and i != 1:
                sum_of_the_proper_divisors += i
            # Add the corresponding divisor greater than sqrt(number)
            corresponding_divisor = number // i
            if corresponding_divisor != number and corresponding_divisor != i:
                sum_of_the_proper_divisors += corresponding_divisor
    # 1 is always a proper divisor (except for 1 itself, which isn't a proper divisor)
    if number != 1:
        sum_of_the_proper_divisors += 1

    if number == sum_of_the_proper_divisors:
        print(number)


6
28
496
8128