Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be “2 2 3”. And if the input number is 315, then output should be “3 3 5 7”.
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 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 above two steps. So print n if it is greater than 2.
3 3 5 7
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 factor 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 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 square root of itself.
This property can be proved using 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 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.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Efficient program to print the number of factors of n numbers
- Maximum number of prime factors a number can have with exactly x factors
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Check if a number exists having exactly N factors and K prime factors
- Count numbers in a given range whose count of prime factors is a Prime Number
- Find number of factors of N when location of its two factors whose product is N is given
- Print all prime factors and their powers
- Super Ugly Number (Number whose prime factors are in given set)
- Number of factors of very large number N modulo M where M is any prime number
- Check if all Prime factors of number N are unique or not
- Product of divisors of a number from a given list of its prime factors
- Generate K co-prime pairs of factors of a given number
- Number with maximum number of prime factors
- Number which has the maximum number of distinct prime factors in the range M to N
- Program to print factors of a number in pairs
- Write an Efficient C Program to Reverse Bits of a Number
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Maximum Length of Sequence of Sums of prime factors generated by the given operations