×

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 Dictionary update() method

Python Dictionary update() method The dictionary.update() method in Python inserts the specified items to the dictionary. Syntax dictionary.update(iterable) Parameter iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will...

1 minute read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python Queue

Python Queue There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for...

7 minutes read.

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

Create a Table 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...

3 minutes read.

How to Configure Python Interpreter in Eclipse

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Python String Variable

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

4 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.