Sure, here is a Python program to check if a given number is a perfect number or not:
Python
def is_perfect_number(number):
"""
Checks if a number is a perfect number.
Args:
number: The number to check.
Returns:
True if the number is a perfect number, False otherwise.
"""
sum_of_factors = 1
for factor in range(2, number):
if number % factor == 0:
sum_of_factors += factor + number // factor
return sum_of_factors == number
if __name__ == "__main__":
number = 6
is_perfect_number = is_perfect_number(number)
print(is_perfect_number)
This program works by first initializing a variable called sum_of_factors to 1. Then, it iterates through the numbers from 2 to
number and adds each factor of number to sum_of_factors. If the sum of the factors is equal to
number, then the number is a perfect number. Otherwise, the number is not a perfect number.
To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as
is_perfect_number.py, you can run it by typing the following command into the command line:
Code snippet
python is_perfect_number.py
This will print the boolean value of whether the number is a perfect number to the console.
Here is an example of the output of the program:
Code snippet
$ python is_perfect_number.py
True
As you can see, the output of the program is True, which means that the number is a perfect number.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Python program to check if a given number is a perfect number or not:
Python
This program works by first initializing a variable called
sum_of_factorsto 1. Then, it iterates through the numbers from 2 tonumberand adds each factor ofnumbertosum_of_factors. If the sum of the factors is equal tonumber, then the number is a perfect number. Otherwise, the number is not a perfect number.To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as
is_perfect_number.py, you can run it by typing the following command into the command line:Code snippet
This will print the boolean value of whether the number is a perfect number to the console.
Here is an example of the output of the program:
Code snippet
As you can see, the output of the program is True, which means that the number is a perfect number.