Learn about Python Program to Print Perfect Numbers in Given Range in the below code example. Also, refer the comments in the code snippet to get a detailed view of what’s actually happening.
Perfect Number: A perfect number is one in which the sum of the factors of a given number equals the same number.
Example :
6 is a perfect number since its divisors are one, two, and three. The sum of the divisors is 1+2+3 = 6.
Contents
Python Program to Print Perfect Numbers in Given Range
Initially, we begin by reading the user’s min_value
and max_value
. The function perfect_Number()
determines whether or not a given number is perfect. We iterate from min_value
to max_value
, passing each integer to the is perfect_Number()
function. We print the result of this function if it returns True.
# Python program to print perfect numbers in an interval
def perfect_Number(n):
if n < 1:
return False
perfect_sum = 0
for i in range(1,n):
if n%i==0:
perfect_sum += i
return perfect_sum == n
min_value = int(input('minimum value: '))
max_value = int(input('maximum value: '))
# perfect numbers
for i in range(min_value, max_value+1):
if perfect_Number(i):
print(i, end=', ')
Output:
minimum value: 1
maximum value: 100
6, 28
Hope the above code works for you and Refer to the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code: Program to Print Perfect Numbers from 1 to 100 in Python
Python Lists