Introduction
In this project, you will learn how to find all the perfect numbers within the range of 1 to 1000. Perfect numbers are positive integers that are equal to the sum of their proper divisors, excluding the number itself.
👀 Preview
## if the range is 1~10, the output should be:
Perfect numbers: 6 ## 6 = 1 + 2 + 3
🎯 Tasks
In this project, you will learn:
- How to implement the
perfect_numberfunction to find all the perfect numbers within a given range - How to test the
perfect_numberfunction and verify the correctness of the results - How to optimize and improve the
perfect_numberfunction for better performance and readability
🏆 Achievements
After completing this project, you will be able to:
- Understand the concept of perfect numbers and their mathematical properties
- Write a function to find all the perfect numbers within a specified range
- Test and debug your code to ensure it works correctly
- Optimize your code for better efficiency and readability
- Apply your problem-solving skills to improve your programming abilities
Understand the Problem
In this step, you will learn about the concept of perfect numbers and the requirements for the project.
Perfect numbers are positive integers that are equal to the sum of their proper divisors, excluding the number itself. For example, the factors of 6 are 1, 2, and 3. Since 6 = 1 + 2 + 3, 6 is a perfect number.
The goal of this project is to:
- Complete the
perfect_numberfunction inperfect_number.pyto find all the perfect numbers within the range of 1 to 1000. - Print out all the perfect numbers found, separated by a space.
Implement the perfect_number Function
Open the perfect_number.py file and locate the perfect_number function. This function is responsible for finding all the perfect numbers within the range of 1 to 1000.
Here's the initial code provided:
def perfect_number():
"""
Find all the perfect numbers within the range of 1 to 1000.
Returns:
list: A list of perfect numbers found within the range of 1 to 1000.
"""
result = []
for num in range(1, 1001):
factors = []
for i in range(1, num):
if num % i == 0:
factors.append(i)
if sum(factors) == num:
result.append(num)
return result
Let's go through the code step by step:
- The function
perfect_numberis defined to find all the perfect numbers within the range of 1 to 1000. - An empty list
resultis initialized to store the perfect numbers. - The function iterates through the numbers from 1 to 1000 (inclusive).
- For each number, the function finds its factors (excluding the number itself) and adds them to the
factorslist. - If the sum of the factors is equal to the number, the number is considered a perfect number and is added to the
resultlist. - Finally, the function returns the
resultlist containing all the perfect numbers found.
Test the perfect_number Function
To test the perfect_number function, add the following code at the end of the perfect_number.py file:
if __name__ == "__main__":
result = perfect_number()
print("Perfect numbers:", " ".join(map(str, result)))
This code will call the perfect_number function and print out all the perfect numbers found, separated by a space.
Save the perfect_number.py file and run the script:
python perfect_number.py
The output should be:
Perfect numbers: 6 28 496
This means that the perfect_number function is working correctly and has found the perfect numbers within the range of 1 to 1000.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



