Competitive Programming in Python |
In this article, we are going to write the code of how to check a number is a perfect number or not in Python.
A natural number is said to be a perfect number if it is the sum of its divisor. For example, 6 is a perfect number because 6 = 1+2+3. but 10 is not a perfect number because 10 != 1 + 2 + 5
If you are looking for a Python tutorial series, then it's here
#perfect number
def Number(n1):
total = 0
for i in range(1,n1):
if n1 % i == 0:
total += i
if total == n1:
print(n1, ' is a perfect number')
else:
print(n1, ' is not a perfect number')
Number(6)
Number(15)
Number(28)
Number(496)
Number(55)
The output of the code is:
6 is a perfect number
15 is not a perfect number
28 is a perfect number
496 is a perfect number
55 is not a perfect number
Comments
Post a Comment