print prime numbers in python tutorials point


Print Prime Numbers In Python Tutorials Point
Print Prime Numbers In Python Tutorials Point

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 : https://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

I am Mr. Sachin pagar the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

google ads

Pythonslearning protected By DMCA

NEWS ADS