Prime number program in python tutorialspoint

In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given a number, we need to check whether the given number is a prime number or not.

A given positive number greater than 1 which has no other factors except 1 and the number itself is referred to as a prime number. 2, 3, 5, 7, etc. are prime numbers as they do not have any other factors.

In this program below, the number is checked about its prime or non-prime nature. Numbers less than or equal to 1 can not be referred to as prime numbers. Hence, we only iterate if the number is greater than 1.

Now we check whether the number is exactly divisible by any number in the range of 2 to[num - 1//2] . If any factor is found in the given range, the number is not prime. otherwise, the number is prime.

Now let’s observe the concept in the implementation below−

Example

num = 17
if num > 1:
   for i in range[2, num//2]:
      # If num is divisible by any number between 2 and n / 2, it is not prime
      if [num % i] == 0:
         print[num, "is not a prime number"]
         break
      else:
         print[num, "is a prime number"]
   else:
print[num, "is not a prime number"]

Output

17 is a prime number

All the variables are declared in the local scope and their references are seen in the figure above.

Conclusion

In this article, we have learned about the python program to check whether the given number is prime in nature or not.

Updated on 23-Dec-2019 07:25:54

  • Related Questions & Answers
  • Python program to check if a number is Prime or not
  • C++ Program to Check Prime Number By Creating a Function
  • C# Program to check if a number is prime or not
  • C++ Program to Check Whether a Number is Prime or Not
  • C Program to Check Whether a Number is Prime or not?
  • PHP program to check if a number is prime or not
  • Java Program to Check Whether a Number is Prime or Not
  • Different Methods to find Prime Number in Python Program
  • Python Program to Check Armstrong Number
  • Program to check whether every rotation of a number is prime or not in Python
  • Bash program to check if the Number is a Prime or not
  • Python Program to Find if a Number is Prime or Not Prime Using Recursion
  • Prime number program in Java.
  • How to check whether a number is prime or not using Python?
  • Write a Golang program to check whether a given number is prime number or not

Print Prime Numbers In Python Tutorials Point

  • Step to Print Prime Numbers In Python Tutorials Point
  • Code for Print Prime Numbers In Python Tutorials Point
  • sachin Pagar

Step to Print Prime Numbers In Python Tutorials Point

● The input is taken from the user.

● “findprime” function is called to find all prime factors and display them.

● Firstly the number is checked to be divisible by 2.

● Divide the number recursively by 2 to remove to as a factor completely.

● Print 2 as a prime factor of the number each time the loop is entered.

● This would help us make our number completely odd.

● Then the condition of prime factor is used. According to which the number will have prime factors up to its square root + 1 only and no more than that.

● So we declare variable “i” as 3.

● Increment it up to square root +1 of the number for our while loop.

● After which the number is checked to be divisible by all odd numbers 3 onwards

● The factors are printed simultaneously.

● The divisibility is checked recursively and simultaneously divided by the same number.

● We have done operations on the number and if none of the conditions are satisfied then we check if it is greater than 2, if yes, then the number is prime. If not then the number is not prime and does not have any prime factors and print error.

● If yes then print the number 

Code for Print Prime Numbers In Python Tutorials Point

import math

#Function to print all prime factors
def findprime[n]:
    
    #Divide by 2 completely to make number odd
    while n % 2 == 0:
        print [2, end=" "]
        n = n / 2
    
    i = 3
    #Using prime factor condition[square root of number] and checking up to that point only
    #as the number won't have any more prime factors further than that
    while i < int[math.sqrt[n]+1]:
        while n % i == 0:
            print [i, end=" "]
            n = n / i
        i = i + 2
    
    #Contition to see if the number is prime
    if n > 2:
        print [int[n], end=" "]
    else:
        print["Invalid number, no prime factors exist."] 

#Take input from user
num = int[input["Enter number to get prime factors: "]]

#Call function to find prime factors
findprime[num]

output :

Enter number to get prime numbers: 96709
The Prime factors are as follows:  97 997

Summary :

In this article we saw Step/ program To Print Prime Numbers In Python Tutorials Point soabout this section you have any query then free to ask me

Name of the Intern who share this Task :

Kunal Goel

Here is the link to my GitHub project : //github.com/kgoel99/Sach-Educational-Support-Tasks

Recommended Article :

  1. Help and dir function in python
  2. Data science in python
  3. Numpy in python

sachin Pagar

I am Mr. Sachin pagar Embedded software engineer, the founder of Sach Educational Support[Pythonslearning], a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

How do you make a prime number in Python?

Step 1: Loop through all the elements in the given range. Step 2: Check for each number if it has any factor between 1 and itself. Step 3: If yes, then the number is not prime, and it will move to the next number. Step 4: If no, it is the prime number, and the program will print it and check for the next number.

What is prime number with example in Python?

A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime [it is composite] since, 2 x 3 = 6 .

Is prime number function Python?

Python Function to Check for Prime Number The above function is_prime[] takes in a positive integer n as the argument. If you find a factor in the specified range of [2, n-1], the function returns False —as the number is not prime. And it returns True if you traverse the entire loop without finding a factor.

How do you write a prime number program?

#include.
int main[]{.
int n,i,m=0,flag=0;.
printf["Enter the number to check prime:"];.
scanf["%d",&n];.
m=n/2;.
for[i=2;i

Chủ Đề