Efficient program to print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then the output should be “2 2 3”. And if the input number is 315, then the output should be “3 3 5 7”.
First Approach:
Following are the steps to find all prime factors.
1) While n is divisible by 2, print 2 and divide n by 2.
2) After step 1, n must be odd. Now start a loop from i = 3 to the square root of n. While i divides n, print i, and divide n by i. After i fails to divide n, increment i by 2 and continue.
3) If n is a prime number and is greater than 2, then n will not become 1 by the above two steps. So print n if it is greater than 2.
C++
// C++ program to print all prime factors #include <bits/stdc++.h> using namespace std; // A function to print all prime // factors of a given number n void primeFactors( int n) { // Print the number of 2s that divide n while (n % 2 == 0) { cout << 2 << " " ; n = n/2; } // n must be odd at this point. So we can skip // one element (Note i = i +2) for ( int i = 3; i <= sqrt (n); i = i + 2) { // While i divides n, print i and divide n while (n % i == 0) { cout << i << " " ; n = n/i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) cout << n << " " ; } /* Driver code */ int main() { int n = 315; primeFactors(n); return 0; } // This is code is contributed by rathbhupendra |
C
// Program to print all prime factors # include <stdio.h> # include <math.h> // A function to print all prime factors of a given number n void primeFactors( int n) { // Print the number of 2s that divide n while (n%2 == 0) { printf ( "%d " , 2); n = n/2; } // n must be odd at this point. So we can skip // one element (Note i = i +2) for ( int i = 3; i <= sqrt (n); i = i+2) { // While i divides n, print i and divide n while (n%i == 0) { printf ( "%d " , i); n = n/i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) printf ( "%d " , n); } /* Driver program to test above function */ int main() { int n = 315; primeFactors(n); return 0; } |
Java
// Program to print all prime factors import java.io.*; import java.lang.Math; class GFG { // A function to print all prime factors // of a given number n public static void primeFactors( int n) { // Print the number of 2s that divide n while (n% 2 == 0 ) { System.out.print( 2 + " " ); n /= 2 ; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for ( int i = 3 ; i <= Math.sqrt(n); i+= 2 ) { // While i divides n, print i and divide n while (n%i == 0 ) { System.out.print(i + " " ); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2 ) System.out.print(n); } public static void main (String[] args) { int n = 315 ; primeFactors(n); } } |
Python
# Python program to print prime factors import math # A function to print all prime factors of # a given number n def primeFactors(n): # Print the number of two's that divide n while n % 2 = = 0 : print 2 , n = n / 2 # n must be odd at this point # so a skip of 2 ( i = i + 2) can be used for i in range ( 3 , int (math.sqrt(n)) + 1 , 2 ): # while i divides n , print i and divide n while n % i = = 0 : print i, n = n / i # Condition if n is a prime # number greater than 2 if n > 2 : print n # Driver Program to test above function n = 315 primeFactors(n) # This code is contributed by Harshit Agrawal |
C#
// C# Program to print all prime factors using System; namespace prime { public class GFG { // A function to print all prime // factors of a given number n public static void primeFactors( int n) { // Print the number of 2s that divide n while (n % 2 == 0) { Console.Write(2 + " " ); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for ( int i = 3; i <= Math.Sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n % i == 0) { Console.Write(i + " " ); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) Console.Write(n); } // Driver Code public static void Main() { int n = 315; primeFactors(n); } } } // This code is contributed by Sam007 |
PHP
<?php // PHP Efficient program to print all // prime factors of a given number // function to print all prime // factors of a given number n function primeFactors( $n ) { // Print the number of // 2s that divide n while ( $n % 2 == 0) { echo 2, " " ; $n = $n / 2; } // n must be odd at this // point. So we can skip // one element (Note i = i +2) for ( $i = 3; $i <= sqrt( $n ); $i = $i + 2) { // While i divides n, // print i and divide n while ( $n % $i == 0) { echo $i , " " ; $n = $n / $i ; } } // This condition is to // handle the case when n // is a prime number greater // than 2 if ( $n > 2) echo $n , " " ; } // Driver Code $n = 315; primeFactors( $n ); // This code is contributed by aj_36 ?> |
Javascript
<script> // JavaScript program to print all prime factors // A function to print all prime // factors of a given number n function primeFactors(n) { // Print the number of 2s that divide n while (n % 2 == 0) { document.write(2 + " " ); n = Math.floor(n / 2); } // n must be odd at this point. // So we can skip one element // (Note i = i +2) for (let i = 3; i <= Math.floor(Math.sqrt(n)); i = i + 2) { // While i divides n, print i // and divide n while (n % i == 0) { document.write(i + " " ); n = Math.floor(n / i); } } // This condition is to handle the // case when n is a prime number // greater than 2 if (n > 2) document.write(n + " " ); } // Driver code let n = 315; primeFactors(n); // This code is contributed by Surbhi Tyagi. </script> |
3 3 5 7
Time Complexity: O(sqrt(n))
In the worst case ( when either n or sqrt(n) is prime, for example: take n=11 or n=121 for both the cases for loop runs sqrt(n) times), the for loop runs for sqrt(n) times. The more number of times the while loop iterates on a number it reduces the original n, which also reduces the value of sqrt(n). Although the best case time complexity is O(log(n)), when the prime factors of n is only 2 and 3 or n is of the form (2^x*(3^y) where x>=0 and y>=0.
Auxiliary Space: O(1)
How does this work?
The steps 1 and 2 take care of composite numbers and step 3 takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers. This is clear that step 1 takes care of even numbers. And after step 1, all remaining prime factors must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2.
Now the main part is, the loop runs till the square root of n not till n. To prove that this optimization works, let us consider the following property of composite numbers.
Every composite number has at least one prime factor less than or equal to the square root of itself.
This property can be proved using a counter statement. Let a and b be two factors of n such that a*b = n. If both are greater than √n, then a.b > √n, * √n, which contradicts the expression “a * b = n”.
In step 2 of the above algorithm, we run a loop and do the following in loop
a) Find the least prime factor i (must be less than √n,)
b) Remove all occurrences i from n by repeatedly dividing n by i.
c) Repeat steps a and b for divided n and i = i + 2. The steps a and b are repeated till n becomes either 1 or a prime number.
Second Approach: This approach is similar to Sieve of Eratosthenes.
We can achieve O(log n) for all composite numbers by consecutive dividing of the given number by an integer starting from 2 representing current factor of that number. This approach works on the fact that all composite numbers have factors in pairs other than 1 or number itself like 6=3 x 2 and 9=3 x 3 whereas for prime numbers there is no such pair other than 1 or the number itself.
Therefore if we start dividing the number by the smallest possible prime number (2) then all of its multiples or composite numbers will automatically be removed before we actually reach that number.
Example: We can divide 12 by 2 two times and remove that factors from 12 to get 3 thus making sure that composite number 4 (multiple of 2) does not occur at any later point of time.
Similarly, if we have a big number that is not divisible by any value of c=2 to n-1 means it is prime like 13 (not divisible from 2 to 12).
C++14
#include <bits/stdc++.h> using namespace std; void primeFactors( int n) { int c=2; while (n>1) { if (n%c==0){ cout<<c<< " " ; n/=c; } else c++; } } /* Driver code */ int main() { int n = 315; primeFactors(n); return 0; } |
C
#include <stdio.h> void primeFactors( int n) { int c = 2; while (n > 1) { if (n % c == 0) { printf ( "%d " , c); n /= c; } else c++; } } /* Driver code */ int main() { int n = 315; primeFactors(n); return 0; } // This code is contributed by Rohit Pradhan |
Java
import java.util.*; class GFG { public static void primeFactors( int n) { int c = 2 ; while (n > 1 ) { if (n % c == 0 ) { System.out.print(c + " " ); n /= c; } else c++; } } /* Driver code */ public static void main(String[] args) { int n = 315 ; primeFactors(n); } } // This code is contributed by Taranpreet |
C#
using System; class GFG { static void primeFactors( int n) { int c = 2; while (n > 1) { if (n % c == 0) { Console.Write(c + " " ); n /= c; } else c++; } } /* Driver code */ public static int Main() { int n = 315; primeFactors(n); return 0; } } // This code is contributed by Taranpreet |
Python3
def primeFactors(n): c = 2 while (n > 1 ): if (n % c = = 0 ): print (c, end = " " ) n = n / c else : c = c + 1 # Driver code n = 315 primeFactors(n) # This code is contributed by Taranpreet |
Javascript
<script> function primeFactors(n) { let c = 2; while (n > 1) { if (n % c == 0){ document.write(c + " " ); n /= c; } else c++; } } /* Driver code */ let n = 315; primeFactors(n); // This code is contributed by singhh3010. </script> |
3 3 5 7
Time Complexity: This Approach is best for all composite numbers and achieves O(log n) but is O(n) otherwise.
Auxiliary Space: O(1)
Related Article :
Prime Factorization using Sieve O(log n) for multiple queries
Thanks to Vishwas Garg for suggesting the above algorithm. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Please Login to comment...