Python is a versatile programming language that can be used for a wide range of applications, including mathematical calculations. One of the most common mathematical problems that programmers encounter is finding the sum of all prime numbers between 1 and 100. In this blog post, we will provide a step-by-step guide on how to write a Python program to solve this problem.
Step 1: Understanding Prime Numbers
Before we dive into coding, it’s essential to understand what prime numbers are. A prime number is a positive integer that is only divisible by 1 and itself. For example, the first few prime numbers are 2, 3, 5, 7, 11, 13, and so on. It’s important to note that 1 is not considered a prime number.
Step 2: Writing the Code
To write a Python program to find the sum of all prime numbers between 1 and 100, we need to first create a function that checks if a number is prime or not. We can then use this function to check all the numbers between 1 and 100 and sum up the prime numbers.
Here’s the code:
def is_prime(num):
"""
Function to check if a number is prime or not
"""
if num == 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
prime_sum = 0
for i in range(1, 101):
if is_prime(i):
prime_sum += i
print("The sum of all prime numbers between 1 and 100 is:", prime_sum)
Let’s break down this code step-by-step:
- We start by defining a function called
is_prime
that takes a number as input and checks if it’s prime or not. - In the function, we first check if the number is equal to 1. If it is, we return False because 1 is not a prime number.
- We then loop through all the numbers from 2 to the square root of the number (plus 1) using the
range
function. This is because if a number is not divisible by any number up to its square root, it’s not divisible by any other number. - Inside the loop, we check if the number is divisible by the current iteration. If it is, we return False because it’s not a prime number.
- If the number is not divisible by any number up to its square root, we return True because it’s a prime number.
After defining the is_prime
function, we create a variable called prime_sum
and set it to 0. This variable will hold the sum of all prime numbers between 1 and 100.
We then use a for
loop to iterate through all the numbers from 1 to 100. Inside the loop, we use the is_prime
function to check if the current number is prime or not. If it is, we add it to the prime_sum
variable.
Finally, we print out the sum of all prime numbers between 1 and 100 using the print
function.
Step 3: Testing the Code
To test our code, we can simply run it and check the output. When we run the code, we should get the following output:
The sum of all prime numbers between 1 and 100 is: 1060
This means that the sum of all prime numbers between 1 and 100 is 1060.
Step 4: Optimizing the Code
While the code we wrote works perfectly fine for finding the sum of all prime numbers between 1 and 100, it’s not the most optimized solution. The is_prime
function checks all the numbers from 2 to the square root of the number, even if the number is not divisible by 2. This means that we’re performing unnecessary calculations, which can slow down our program if we’re dealing with larger numbers.
To optimize our code, we can first check if the number is even and return False if it is, except for 2. This is because all even numbers, except for 2, are not prime. We can then iterate through all the odd numbers between 3 and the square root of the number, checking only odd numbers to see if the number is divisible by any of them.
Here’s the optimized code:
def is_prime(num):
"""
Function to check if a number is prime or not
"""
if num == 2:
return True
if num % 2 == 0 or num == 1:
return False
for i in range(3, int(num ** 0.5) + 1, 2):
if num % i == 0:
return False
return True
prime_sum = 0
for i in range(1, 101):
if is_prime(i):
prime_sum += i
print("The sum of all prime numbers between 1 and 100 is:", prime_sum)
Let’s break down the changes we made to the code:
- In the
is_prime
function, we first check if the number is equal to 2. If it is, we return True because 2 is a prime number. - We then check if the number is even and return False if it is, except for 2. This means that all even numbers, except for 2, are not prime.
- We then loop through all the odd numbers from 3 to the square root of the number (plus 1) using the
range
function with a step size of 2. This means that we’re only checking odd numbers to see if the number is divisible by any of them.
By making these changes, we’re only checking the necessary numbers, which can significantly improve the performance of our program.
Follow us on Twitter: Hacktube5
Follow us on YouTube: Hacktube5