Sum of Prime Numbers Under 100

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to implement a function to check if a number is prime, and then use that function to calculate the sum of all prime numbers below 100.

👀 Preview

>>> prime(1)
False
>>> prime(6)
False
>>> prime(7)
True
>>> prime_total()
1060

🎯 Tasks

In this project, you will learn:

  • How to write a function to check if a number is prime
  • How to use that function to calculate the sum of all prime numbers below 100

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept of prime numbers
  • Write a function to check if a number is prime
  • Use that function to solve a problem involving prime numbers
  • Apply your programming skills to a real-world problem

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/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/numeric_types -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/booleans -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/type_conversion -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/conditional_statements -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/for_loops -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/tuples -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/function_definition -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/python_shell -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} python/build_in_functions -.-> lab-302778{{"`Sum of Prime Numbers Under 100`"}} end

Implement the prime Function

In this step, you will implement the prime function, which checks if a given number is a prime number or not.

  1. Open the prime_total.py file in the /home/labex/project directory.
  2. Locate the prime function definition:
def prime(n: int) -> bool:
    """
    Check if a number is prime.

    Args:
        n (int): The number to be checked.

    Returns:
        bool: True if the number is prime, False otherwise.
    """
  1. Implement the logic to check if the given number n is a prime number. A number is prime if it is greater than 1 and has no factors other than 1 and itself.
  2. Your implementation should look like this:
def prime(n: int) -> bool:
    """
    Check if a number is prime.

    Args:
        n (int): The number to be checked.

    Returns:
        bool: True if the number is prime, False otherwise.
    """
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

Implement the prime_total Function

In this step, you will implement the prime_total function, which calculates the sum of all prime numbers below 100.

  1. Locate the prime_total function definition:
def prime_total() -> int:
    """
    Calculate the sum of all the prime numbers below 100.

    Returns:
        int: The sum of all the prime numbers below 100.
    """
  1. Implement the logic to calculate the sum of all prime numbers below 100. You can use the prime function you implemented in the previous step to check if a number is prime.
  2. Your implementation should look like this:
def prime_total() -> int:
    """
    Calculate the sum of all the prime numbers below 100.

    Returns:
        int: The sum of all the prime numbers below 100.
    """
    total = 0
    for num in range(2, 100):
        if prime(num):
            total += num
    return total

Test the Implementation

  1. At the end of the prime_total.py file, add the following code to test the implementation:
if __name__ == "__main__":
    print(prime_total())
  1. Save the prime_total.py file.
  2. Open a terminal and navigate to the /home/labex/project directory.
  3. Run the prime_total.py file using the following command:
python prime_total.py
  1. The output should be the sum of all prime numbers below 100, which is 1060.

Examples:

>>> prime(1)
False
>>> prime(6)
False
>>> prime(7)
True
>>> prime_total()
1060

Congratulations! You have successfully implemented the prime and prime_total functions to calculate the sum of all prime numbers below 100.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like