Perfect Number Finder Project

PythonPythonBeginner
Practice Now

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_number function to find all the perfect numbers within a given range
  • How to test the perfect_number function and verify the correctness of the results
  • How to optimize and improve the perfect_number function 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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/variables_data_types -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/strings -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/conditional_statements -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/for_loops -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/lists -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/tuples -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/function_definition -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/data_collections -.-> lab-302754{{"`Perfect Number Finder Project`"}} python/build_in_functions -.-> lab-302754{{"`Perfect Number Finder Project`"}} end

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:

  1. Complete the perfect_number function in perfect_number.py to find all the perfect numbers within the range of 1 to 1000.
  2. 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:

  1. The function perfect_number is defined to find all the perfect numbers within the range of 1 to 1000.
  2. An empty list result is initialized to store the perfect numbers.
  3. The function iterates through the numbers from 1 to 1000 (inclusive).
  4. For each number, the function finds its factors (excluding the number itself) and adds them to the factors list.
  5. If the sum of the factors is equal to the number, the number is considered a perfect number and is added to the result list.
  6. Finally, the function returns the result list 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.

Other Python Tutorials you may like