14.0k views
3 votes
write a program to determine whether a number n is equal to the sum of its proper positive divisors excluding the number itself

User Mambax
by
7.6k points

1 Answer

0 votes

PROGRAM

Here is a program in Python that determines whether a number n is equal to the sum of its proper positive divisors (excluding the number itself):

def is_perfect(n):

divisors = []

for i in range(1, n):

if n % i == 0:

divisors.append(i)

if sum(divisors) == n:

return True

else:

return False

n = int(input("Enter a number: "))

if is_perfect(n):

print(n, "is a perfect number.")

else:

print(n, "is not a perfect number.")

The function is_perfect(n) takes an integer n as input, and uses a for loop to find all the positive divisors of n, excluding n itself. It then checks if the sum of these divisors is equal to n. If it is, the function returns True, indicating that n is a perfect number. If the sum of the divisors is not equal to n, the function returns False, indicating that n is not a perfect number.

The program prompts the user to enter a number, then calls the is_perfect(n) function to check if the number is a perfect number. If it is, the program prints that the number is a perfect number, otherwise it prints that the number is not a perfect number.

It's important to note that there are not many perfect numbers, and for larger numbers this algorithm could become inefficient. In case you need to work with large numbers, it is more efficient to use the Sieve of Eratosthenes algorithm to generate the perfect numbers.

Hope This Helps You!

User Alex Tarasenko
by
7.0k points