We can calculate the prime factorization of a number “n” in O(sqrt(n)) as discussed here. But O(sqrt n) method times out when we need to answer multiple queries regarding prime factorization.
In this article we study an efficient method to calculate the prime factorization using O(n) space and O(log n) time complexity with per-computation allowed.
Key Concept: Our idea is to store the Smallest Prime Factor(SPF) for every number. Then to calculate the prime factorization of the given number by dividing the given number recursively with its smallest prime factor till it becomes 1.
To calculate to smallest prime factor for every number we will use the sieve of eratosthenes. In original Sieve, every time we mark a number as not-prime, we store the corresponding smallest prime factor for that number (Refer this article for better understanding).
Now, after we are done with precalculating the smallest prime factor for every number we will divide our number n (whose prime factorziation is to be calculated) by its corresponding smallest prime factor till n becomes 1.
Pseudo Code for prime factorization assuming SPFs are computed : PrimeFactors // To store result i = 0 // Index in PrimeFactors while n != 1 : // SPF : smallest prime factor PrimeFactors[i] = SPF[n] i++ n = n / SPF[n]
The implementation for the above method is given below :
prime factorization for 12246 : 2 3 13 157
Note : The above code works well for n upto the order of 10^7. Beyond this we will face memory issues.
Time Complexity: The precomputation for smallest prime factor is done in O(n log log n) using sieve. Where as in the calculation step we are dividing the number every time by the smallest prime number till it becomes 1. So, let’s consider a worst case in which every time the SPF is 2 . Therefore will have log n division steps. Hence, We can say that our Time Complexity will be O(log n) in worst case.
This article is contributed by Nitish Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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.
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Sum of Factors of a Number using Prime Factorization
- Generating all divisors of a number using its prime factorization
- Longest sub-array of Prime Numbers using Segmented Sieve
- Pollard's Rho Algorithm for Prime Factorization
- Trial division Algorithm for Prime Factorization
- Sum of all Primes in a given range using Sieve of Eratosthenes
- Fermat's Factorization method for large numbers
- Fermat's Factorization Method
- Dixon's Factorization Method with implementation
- Wheel Factorization Algorithm
- Euler's Factorization method
- Sieve of Eratosthenes
- Segmented Sieve
- Sieve of Sundaram to print all primes smaller than n
- Sieve of Atkin
- Sieve of Eratosthenes in 0(n) time complexity
- Number of unmarked integers in a special sieve
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Bitwise Sieve