In: Computer Science
n = int(input("Enter any number: ")) sum1 = 0 for i in range(1, n): if(n % i == 0): sum1 = sum1 + i if (sum1 == n): print("The number is a Perfect number!") else: print("The number is not a Perfect number!")
taking the code above, how would i create a new function that calls the program above and finds all perfect numbers within a certain range? the function would accept two parameters, the two ranges, and use a for loop to call the function above and print out all perfect numbers within a certain range (Ex. 1,100)
The answer to this question is as follows:
The code is as follows:
def list_perfect_numbers(range1,range2):
for i in range(range1,range2):
sum1=0
for j in range(1,i):
if(i%j==0):
sum1+=j
if(sum1==i):
print(i)
range1=int(input())
range2=int(input())
if(range1<range2):
list_perfect_numbers(range1,range2)
else:
print("Spcify the correct Range")
Note: The range should be in the range1<range2
The input and ouput are provided in the screenshot below: