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.
- Open the
prime_total.pyfile in the/home/labex/projectdirectory. - Locate the
primefunction 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.
"""
- Implement the logic to check if the given number
nis a prime number. A number is prime if it is greater than 1 and has no factors other than 1 and itself. - 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.
- Locate the
prime_totalfunction 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.
"""
- Implement the logic to calculate the sum of all prime numbers below 100. You can use the
primefunction you implemented in the previous step to check if a number is prime. - 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
- At the end of the
prime_total.pyfile, add the following code to test the implementation:
if __name__ == "__main__":
print(prime_total())
- Save the
prime_total.pyfile. - Open a terminal and navigate to the
/home/labex/projectdirectory. - Run the
prime_total.pyfile using the following command:
python prime_total.py
- 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.



