Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
In this post, we’ll look at a Python program that prints out all the prime factors of a given number. A number is considered to be a prime factor of another number if it is a prime number and perfectly divides the given number. In this section, we will look at what a prime factor is, how to discover a prime factor, and the python program.
A number’s prime factors are the prime numbers that, when multiplied together, give the number. Two conditions can be used to determine a number’s prime factor:
The digit should be a prime number.
The number should perfectly divide the number.
- Python Program to Find Smallest Prime Divisor of a Number
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
Examples:
Example1:
Input:
given number =240
Output:
The prime factors of the given number are : 2 3 5
Example2:
Input:
given number =33
Output:
Enter some random number = 33 The prime factors of the given number are : 3 11
Program to Compute Prime Factors of an Integer in Python
There are several ways to compute prime factors of an integer in python some of them are:
Method #1: Using while loop (Static Input)
Approach:
- Give the integer value as static input.
- Using a while loop, first determine the number’s components.
- Calculate whether the factors are prime or not using another while loop within the previous one.
- Exit of program.
Below is the implementation:
# given number numb = 240 # Printing the prime factors print("The prime factors of the given number are : ") value = 1 while(value <= numb): k = 0 if(numb % value == 0): j = 1 while(j <= value): if(value % j == 0): k = k+1 j = j+1 if(k == 2): # printing the prime factor print(value) # incremeent the value by 1 value = value+1
Output:
The prime factors of the given number are : 2 3 5
Explanation:
- Given the number as static input.
- The while loop is utilized, and the integer factors are obtained by using the modulus operator and determining whether the remainder of the number divided by value is zero.
- The integer’s factors are then checked again to see if they are prime.
- The factor of an integer is prime if it has two factors.
- The integer’s prime factor is printed.
Method #2: Using while loop (User Input)
Approach:
- Scan the number using int(input()) function.
- Using a while loop, first determine the number’s components.
- Calculate whether the factors are prime or not using another while loop within the previous one.
- Exit of program.
Below is the implementation:
# Scan the number using int(input()) function. numb = int(input("Enter some random number = ")) # Printing the prime factors print("The prime factors of the given number are : ") value = 1 while(value <= numb): k = 0 if(numb % value == 0): j = 1 while(j <= value): if(value % j == 0): k = k+1 j = j+1 if(k == 2): # printing the prime factor print(value) # incremeent the value by 1 value = value+1
Output:
Enter some random number = 220 The prime factors of the given number are : 2 5 11
Method #3:Efficient Method (User input)
Approach:
- Scan the given number using int(input()) function.
- We will print 2 and divide numb by 2 while it is divisible by 2.
- Following step 2, numb must always be odd.
- Begin a loop with I = 3 and work your way up to the square root of n. If I divide numb by I print I and divide numb by i. If we unable to divide numb , increase the I value by 2 and proceed.
- If numb is a prime number and higher than 2, it cannot be reduced to 1.
- So, if numb is larger than 2, print it.
Below is the implementation:
import math # Scan the number using int(input()) function. numb = int(input("Enter some random number = ")) # Printing the prime factors print("The prime factors of the given number are : ") while numb % 2 == 0: print(2), numb = numb / 2 # n became odd for i in range(3,int(math.sqrt(numb))+1,2): while (numb % i == 0): print (int(i)) numb = numb / i if numb > 2: print (int(numb))
Output:
Enter some random number = 33 The prime factors of the given number are : 3 11
Related Programs:
-
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
- Python Program to Print all the Prime Numbers within a Given Range
- Python Program to Find the Factors of a Number
- Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers