In [9]:
import math
n = int (input('Ente the number: '))
num = n
sum = 1
for i in range (2, int (math.sqrt(n)) + 1):
if n % i == 0:
r = int (n / i)
if r * r == n:
sum = sum + r;
else:
sum = sum + i + r
if sum == num:
print (num, ' is a perfect number.')
else:
print (num, ' is not a perfect number. Because the summation of divisor is: ', sum)
Ente the number: 8128 8128 is a perfect number.
In [10]:
n = 1
m = 8128
print ("Perfect numbers from ", n, " to ", m, ": ")
for x in range (n, m + 1):
sum = 1
for i in range (2, (int (math.sqrt(x))) + 1):
if x % i == 0:
r = int (x / i)
if r * r == x:
sum = sum + r
else:
sum = sum + r + i
if sum == x:
print (x, end = ' ')
Perfect numbers from 1 to 8128 : 1 6 28 496 8128
No comments:
Post a Comment