5

Python program to print all Prime numbers in an Interval

 1 year ago
source link: https://www.geeksforgeeks.org/videos/python-program-to-print-all-prime-numbers-in-an-interval/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python program to print all Prime numbers in an Interval

Python program to print all Prime numbers in an Interval
Hello everyone and welcome to week 4 geek. So
  • 40 Views
  • 29/08/2022

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/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK