Prime number program in python (4 different way)

In this article, we are going to study ” Prime number program in python in three different ways”.

What is a Prime Number?

A positive integer greater than 1 which does not have other factors except 1 and the number itself is called a prime number. The numbers 2, 3, 5, 7, etc. are prime numbers as they do not have any other factors. To find a prime number in Python, you have to iterate the value from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.

Prime number program in python

There are different ways to optimize the prime number.

1. Python Program to Check Prime Number

A prime number is always positive and it will be checked at the beginning of the program. Here, you will divide the input number by all the numbers to see whether there are any positive divisors other than 1 and number itself. If any divisor is found then we display that the “number is not a prime number” else we display that the “number is a prime number”.

Example to check whether an integer is a prime number or not using for loop and if…else statement.

Source code:

num=int(input("Enter prime no: "))
if num > 1 :
  for n in range(2, num):
    if  (num % n) == 0 :
      print(f"{num} is not Prime Number")
      break
  else:
    print(f"{num} is Prime Number")    
else:
  print(f"{num} not Prime Number")  
Output-
Enter prime no: 56
56 is not Prime Number

2. Python Program to Print all Prime Numbers in an Interval

In this program, you’ll learn to print all prime numbers within an interval using for loops and display it.

Here, we store the interval as num1 for lower interval and num2 for upper interval and find prime numbers in that range. Visit this page to learn how to check whether a number is prime or not.

Source Code:

num1 = int(input("Enter first interval: "))
num2 = int(input("Enter second interval: "))
for n in range(num1, num2):
  for num2 in range(2,n):
    if  n % num2 == 0 :
      break
  else:
    print(n)    
Output-
Enter first interval: 10
Enter second interval: 20
11
13
17
19

3. Python to Print Prime Numbers and not Prime Number

In this program, you’ll learn to print all prime numbers and not Prime number at the same time using for loops and display it.

num2 = int(input("Enter Number: "))
for n in range(2, num2):
  for num2 in range(2,n):
    if  n % num2 == 0 :
      print(f"{n} = Not Prime")
      break
  else:
      print(f"{n} = Prime")    
Output-
Enter Number: 10
2 = Prime
3 = Prime
4 = Not Prime
5 = Prime
6 = Not Prime
7 = Prime
8 = Not Prime
9 = Not Prime

4. Python Program to Print all Prime Numbers using list comprehension

n=int(input())
primes = [i for i in range(2,n) if all(i%j !=0 for j in range(2,int(i**0.5) + 1))]
print(primes)

Output:-

100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Source:- Programiz

Also View:- List & tuple & set & dictionary in Python

So these were some of the different ways to optimize the prime number. Let us know in the comment section if you know any extra way to solve. 

About Ashishkumar Vishwakarma

I am Ashish- a Developer live in Mumbai.

View all posts by Ashishkumar Vishwakarma →

Leave a Reply