• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
August 29, 2022 |2.6K Views
Python program to print all Prime numbers in an Interval
  Share   Like
Description
Discussion

In this video, we will write a Python program to print all Prime numbers in n interval.

In this video, we have discussed 2 approaches:
1. Using For Loop
2. Using sqrt() function

Approach 1: Using For loop: In this approach, we take user input of 2 integers as the upper and lower limit of the interval. Then we initialize an empty list to store prime numbers.

Then we iterate on each integer in the interval and check if it's divisible by any other number except 1 and itself, else we append it to the list of prime numbers. At the end of the loop, we print all prime numbers and count of prime numbers in the interval from the list of prime numbers formed.

Approach 2: Using math.sqrt() function: This approach is used to optimize the checking of prime numbers for numbers even in long range also. 

In this approach, we iterate on each number in the interval and skip if the number is either divisible by 2 or not divisible by all numbers in the range of 3 to the square root of the number. Else, we append the number to the list of prime numbers. At the end of the loop, we print all the prime numbers and the count of prime numbers from the generate list of prime numbers.

Python program to print all Prime numbers in an Interval
https://www.geeksforgeeks.org/python-program-to-print-all-prime-numbers-in-an-interval/