Prime Factorization using Sieve O(log n) for multiple queries in C++

C++Server Side ProgrammingProgramming

In this problem, we need to create a program to calculate Prime Factorization using Sieve O(log n) for multiple queries. 

As the general method takes O(sqrt(n) ) time which will increase the time required to a huge extent from multiple queries.

Let’s recap first,

Prime factorization of a number includes ONLY the prime factors, not any products of those prime factors.

Sieve of Eratosthenes is an algorithm to generate all prime numbers within the given range.

Solution Approach

The solution to the problem is found by finding the smallest factor that divides the number, saving it as a factor and updating the number by dividing it by the factor. This process is done recursively till the number becomes 1 after division, which means no other factors are possible.

The calculation is done using sieve of eratosthenes which reduces the time complexity in finding the smallest prime factor.

Program to illustrate the working of our solution

Example

Live Demo

#include <iostream>
using namespace std;
int primes[100001];

void sieveOfEratosthenes(int N) {
   
   N+=2;
   primes[1] = 1;
   for (int i=2; i<N; i++)
      primes[i] = i;
   for (int i=4; i<N; i+=2)
      primes[i] = 2;
   for (int i=3; i*i<N; i++) {
      if (primes[i] == i) {
         for (int j=i*i; j<N; j+=i)
            if (primes[j]==j)
               primes[j] = i;
      }
   }
}
void findPrimeFactors(int num) {
   
   sieveOfEratosthenes(num);
   int factor;
   while (num != 1) {
      factor = primes[num];
      cout<<factor<<" ";
      num /= factor;
   }
}

int main() {
   int N = 45214;
   cout<<"Prime factorization of the number "<<N<<" using sieve is ";
   findPrimeFactors(N);
   return 0;
}

Output

Prime factorization of the number 45214 using sieve is 2 13 37 47
raja
Published on 27-Jan-2021 05:05:09
Advertisements