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

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.

âœĻ Check Solution and Practice

Summary

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

Other Python Tutorials you may like