- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Prime Factorization using Sieve O(log n) for multiple queries in C++
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
#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
- Related Articles
- Program for power of a complex number in O(log n) in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Evaluate: $log sin1^o.log sin 2^o. log sin3^o ........log sin90^o$.
- Pollard’s Rho Algorithm for Prime Factorization in java
- Queries for maximum difference between prime numbers in given ranges in C++
- Write an iterative O(Log y) function for pow(x, y) in C++
- Queries to count the number of unordered co-prime pairs from 1 to N in C++
- Program to compute Log n in C
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- What is prime factorization?
- Prime factorization of 246000
- Queries to Print All the Divisors of n using C++
- Java Program to get prime numbers using the Sieve of Eratosthenes algorithm
- C program to display all prime numbers between 1 to N using for loop
- Find minimum number of Log value needed to calculate Log upto N in C++
