
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Efficient Program to Print All Prime Factors of a Given Number in Python
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 find all the prime factors of a given number.
The efficient solution to the problem is discussed below −
Example
# Python program to print prime factors import math # prime def primeFactors(n): # no of even divisibility while n % 2 == 0: print (2), n = n / 2 # n reduces to become odd for i in range(3,int(math.sqrt(n))+1,2): # while i divides n while n % i== 0: print (i) n = n / i # if n is a prime if n > 2: print (n) n = 200 primeFactors(n)
Output
2 2 2 5 5
All the variables and functions are declared in the global scope as shown in the figure above.
Conclusion
In this article, we have learned how we can print all prime factors of a given number efficiently.
Advertisements