×

Python Prime factorization

Python Prime factorization

In this tutorial, we will design a program where we will find all the prime factors of a number. Then, we will print all these prime factors of the given number in the output of the number.

A Python program to find all the prime factors of any number

Before proceeding with the Python program, look at the following example:

  • Prime factor of 105: 3, 5, 7
  • Prime factor of 246: 2, 3, 41 etc.

Following are the key steps for program:

1). When the given number is divisible by 2, we should print 2 in output and divide the number by 2. We have to follow this step until the number is no more divisible by 2.

2). After completing the step 1, the given number is now must be odd. Now, we have to start for loop on the resultant number from step 1, from i = 3 to square root of the given number. Print i integer in output everytime when i divides the resultant number.

3). When number is not divisible by i, print the remaining resultant number in the output.

This will be the prime factors of a given number in the program.

Note

We also have to add a condition where the number given by user maybe a prime number in itself. So, for this case we have to write if condition statement in the program where the given number should be greater than 1.

Now, look at the following program to find all the prime factors of a number and after running the following program we will understand it's working and functioning in detail:

Example –

 # Importing math module in Python
 import math
 # Design a function PrimeFactorization for prime factors of number
 def PrimeFactorization(a):
 # Use the while loop for checking divisibility by 2
             while a % 2 == 0:
                         print (2), # printing 2 until condition become false
                         a = a / 2
             # Using a for loop withsqrt() function of math module  
             for b in range(3,int(math.sqrt(a))+1,2):
 # Using a while loop inside for loop
                         while a % b == 0:
                                     print (b), # printing all odd prime factors
                                     a = a / b
             # Using the if condition
             if a > 2:
                         print (a) # if condition satisfies it will print original number
 # Take a positive prime number from user
 a = int(input("Enter a positive integer number: "))
 # Print all prime factors of given number
 print("The prime factors are : ")
 PrimeFactorization(a) # calling out PrimeFactorization function 

Output:

 Enter a positive integer number: 25515
 The prime factors are:
 3
 3
 3
 3
 3
 3
 5
 7 

Working of the program:

Now, let's understand how the program works so that we know how with this program Python finding the prime factors of any given number.

  • We designed a PrimeFactorization function, where we perform operations on number given by user.
  • First, we used a while loop to follow the step 1, and divided the number by 2 until the condition 'a % 2 = 0' becomes false. We printed the 2 in output everytime while loop runs i.e., print(a).
  • When the condition becomes false, it means we know that now the resultant number is odd.
  • Now, we used for loop in the second step and assigned a = b. Then, we used the while loop to follow the condition, from b = 3 to square root of a. It will print all the odd prime factors of number a with print(b) statement.
  • After that, we have also used to if statement condition to check the condition that given number by user is greater than 1.
  • Then, we defined variable a and store the number in it that user will provide while running the program.
  • After that, we called out the PrimeFactorization function and provided argument 'a' in the function. It will print all the prime factors of number in the output when we run the program.

Related Topics

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Anonymous/Lambda Function in Python

Lambda keyword is used to declare an Anonymous function, i.e. a function that does not have any name. It is also called Anonymous functions. In python, normal functions are defined...

3 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Create Table Using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With this library's...

4 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.