Check Whether or Not the Number is a Perfect Number in Python
Given an integer input as a number, the objective is to check whether or not a number is a Perfect Number in Python Language. Therefore, we write a program to Check Whether or Not the Number is a Perfect Number in Python Language.
Example
Input : 28
Divisors : [1, 2, 4, 7, 14]
Sum = 1 + 2 + 4 + 7 + 14 = 28
Output : It's a Perfect Number
Perfect NumberA Number that can be represented as the sum of all the factors of the number is known as a Perfect Number.
Let's Try and understand it better using an example
Example
Input : 28
Output : It's a Perfect Number
Explanation : Number = 28
28 = 1 + 2 + 14 + 4 + 7
as the number 28 has factors 1, 2, 4, 7 and 14.
We sum them up and check whether they match the original number.
As we can see from the above example, number 28 is a Perfect Number.
Make sure you don't include the number itself as a factor.
n = 28
sum = 0
for i in range(1, n):
if n % i == 0:
sum = sum + i
if sum == n:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
num = 28
sum = 0
i = 1
while i < num:
if num % i == 0:
sum += i
i += 1
if sum == num:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
Output
The number is a Perfect number
Method 3: Iteration between [1, num/2+1]
This method uses fact that all the divisors of the number can be found in the range (1, num/2)
num = 28
sum = 0
for i in range(1, num//2 + 1):
if num % i == 0:
sum = sum + i
if sum == num:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
Output
The number is a Perfect number
Method 4: Using recursion
We use recursion to find if the number is perfect or not.
sum_n = 0
def getSumDivisors(num, i):
global sum_n
# since, all factors can be found will num/2
# we will only check in this range
if i <= num // 2:
if num % i == 0:
sum_n = sum_n + i
i += 1
# recursively call isPerfect
getSumDivisors(num, i)
# returns the sum
# when i becomes > num/2
return sum_n
num = 28
if getSumDivisors(num, 1) == num:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
Output
The number is a Perfect number
Method 5: Factors come in pairs
This method uses observations that all factors come in pairs.
All Factors come in pairsFor n = a * b (For each a there exists a unique b)
n = 28
sump = 0
for i in range(1, int(pow(n, 0.5))):
if n % i == 0:
# For n : (1, n) will always be pair of divisor
# acc to def., we must ignore adding num itself as divisor
# when calculating for perfect number
if i == 1:
sump += i
# Example For 100 (10,10) will be one pair
# But, we should add value to the sum just once
elif i == n / i:
sump += i
# add both pairs as divisors
# For any divisor i, n/i will also be a divisor
else:
sump += i + n / i
if sump == n:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
# Time complexity: O(sqrt(N))
# Space complexity: O(1)
Output
The number is a Perfect number
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
n=int(input())
a=0
i = 1
while i < n:
if (n % i == 0):
a+=i
i = i + 1
if a==n:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
n1 = int(input())
k = 0
for i in range(1, n1):
if n1%i == 0:
k = k + i
if k == n1:
print(‘Perfect number {}’.format(n1))
else:
print(‘Not perfect number{}’.format(n1))
n=int(input())
a=0
i = 1
while i < n:
if (n % i == 0):
a+=i
i = i + 1
if a==n:
print("The number is a Perfect number")
else:
print("The number is not a Perfect number")
Hey !!
We’d suggest you to join our Discord Channel for all your technical queries.
n=int(input(“enter a number : “))
l=[i for i in range(1,n) if(n%i==0)]
if(sum(l)==n):
print(“Perfect number”)
else:
print(“not a perfect number”)
n1 = int(input())
k = 0
for i in range(1, n1):
if n1%i == 0:
k = k + i
if k == n1:
print(‘Perfect number {}’.format(n1))
else:
print(‘Not perfect number{}’.format(n1))
a=int(input())
c=[]
s=0
for i in range(1,a):
if(a%i==0):
c.append(i)
for i in c:
s+=i
if(s==a):
print(“perect”)
else:
print(“not perfect”)