Prime factors of a number in Python
In this post, you will learn a Python program to print prime factors of a number. Before starting the coding part, it should be clear about the prime number and prime factor.
A prime number is a whole number greater than 1, whose only factors are 1 and itself. The prime numbers, which divide the given number perfectly, are known as the prime factors of that number. Basically, it is finding prime numbers that prime numbers multiply together to make the original number. The prime factorization is useful when working with big numbers, such as in cryptography, or it is commonly used in mathematical problems to secure public-key encryption systems. For example, if the input number is 110, then the output should be "2 5 11". And if the input number is 99, then the output should be "3 11". With the help of these two conditions, we can check whether a number is a prime factor of a given number or not.
- The number must be a prime number.
- The number must perfectly divide the given number.
Python program to find prime factors using for loop
Here is the program to find the prime factors of a number using Python. This program allows the user to enter any positive integer using the input() method. Next, Python returns the prime factors of that number using the nested for loop.
# Python Program to find Prime Factors of a Number
num = int(input(" Please enter any number: "))
for x in range(2, num + 1):
if(num % x == 0):
chk_prime = 1
for y in range(2, (x //2 + 1)):
if(x % y == 0):
chk_prime = 0
break
if (chk_prime == 1):
print(" %d is a Prime Factor of the given number %d" %(x, num))
Output of the above code-In the above example, we have used the for loop only. Firstly, we have taken the input from the user using the input() method and stored it in a variable num. Then, we have applied the for loop from x = 2 to x = num+1. Next, we check if the modulo of the value of x and number is equal to 0. Then we keep the count value = 1 and again apply the for loop inside the for loop from y = 2 to y = x//2+1. and check the given if condition. If the condition is satisfied, the count value is set to 0 and we break the statement. Then we come out of the for loop and check the condition if count ==1 and print the value of x. Hence the prime factors are printed with their single-single value.
Python Program to find prime factors using while loop
Here, we take a number from user input and store it in a variable. Next, we obtain the factors of a number with the help of a nested while loop and find whether the factors are prime or not.
# Python Program to find Prime Factors of a Number
num = int(input(" Please enter any number: "))
x = 1
while(x <= num):
count = 0
if(num % x == 0):
y = 1
while(y <= x):
if(x % y == 0):
count = count + 1
y = y + 1
if (count == 2):
print(" %d is a Prime Factor of the given number %d" %(x, num))
x = x + 1
Output of the above code -Python Program to find prime factors using while loop and for loop
In the given Python program, we have used the while loop and for loop, both for finding out the prime factors of the given number.
# Python Program to find Prime Factors of a Number
import math
def primefactors(num):
#even number divisible
while num % 2 == 0:
print(2),
num = num / 2
#n became odd
for x in range(3,int(math.sqrt(num))+1,2):
while (num % x == 0):
print(x)
num = num / x
# if num is prime
if num > 2:
print(num)
num = int(input("Please enter any number : "))
primefactors(num)
Output of the above code -Related Articles
Python multiline string
glob in Python
Python heap implementation
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module
Image to text python
Python Random shuffle() Method
Fibonacci Series In Python | Python Program To Print
File Handling in Python
Python - XML to JSON
Python XML to Dictionary
Serialize Python dictionary to XML