In this article, we study an optimized way to calculate the distinct prime factorization up to n natural number using O **O(n*log n)** time complexity with pre-computation allowed.

**Prerequisites :** Sieve of Eratosthenes, Least prime factor of numbers till n.

Key Concept:Our idea is to store the Smallest Prime Factor(SPF) for every number. Then to calculate the distinct 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).

The implementation for the above method is given below :

## C++

`// C++ program to find prime factorization upto n natural number ` `// O(n*Log n) time with precomputation ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAXN 100001 ` ` ` `// Stores smallest prime factor for every number ` `int` `spf[MAXN]; ` ` ` `// Adjacency vector to store distinct prime factors ` `vector<` `int` `>adj[MAXN]; ` ` ` `// Calculating SPF (Smallest Prime Factor) for every ` `// number till MAXN. ` `// Time Complexity : O(nloglogn) ` `void` `sieve() ` `{ ` ` ` `spf[1] = 1; ` ` ` `// marking smallest prime factor for every ` ` ` `// number to be itself. ` ` ` `for` `(` `int` `i=2; i<MAXN; i++) ` ` ` `spf[i] = i; ` ` ` ` ` ` ` `for` `(` `int` `i=2; i*i<MAXN; i++) ` ` ` `{ ` ` ` `// checking if i is prime ` ` ` `if` `(spf[i] == i) ` ` ` `{ ` ` ` `// marking SPF for all numbers divisible by i ` ` ` `for` `(` `int` `j=i*i; j<MAXN; j+=i) ` ` ` ` ` `// marking spf[j] if it is not ` ` ` `// previously marked ` ` ` `if` `(spf[j]==j) ` ` ` `spf[j] = i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// A O(nlog n) function returning distinct primefactorization ` `// upto n natural number by dividing by smallest prime factor ` `// at every step ` `void` `getdistinctFactorization(` `int` `n) ` `{ ` ` ` `int` `index,x,i; ` ` ` `for` `(` `int` `i=1;i<=n;i++) ` ` ` `{ ` ` ` `index=1; ` ` ` `x=i; ` ` ` `if` `(x!=1) ` ` ` `adj[i].push_back(spf[x]); ` ` ` `x=x/spf[x]; ` ` ` `// Push all distinct prime factor in adj ` ` ` `while` `(x != 1) ` ` ` `{ ` ` ` `if` `(adj[i][index-1]!=spf[x]) ` ` ` `{ ` ` ` `adj[i].push_back(spf[x]); ` ` ` `index+=1; ` ` ` `} ` ` ` `x = x / spf[x]; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Precalculating smallest prime factor ` ` ` `sieve(); ` ` ` ` ` `int` `n = 10; ` ` ` ` ` ` ` `getdistinctFactorization(n); ` ` ` ` ` `// Print the prime count ` ` ` `cout <<` `"Distinct prime factor for first "` `<< n ` ` ` `<<` `" natural number"` `<<` `" : "` `; ` ` ` ` ` `for` `(` `int` `i=1; i<=n; i++) ` ` ` `cout << adj[i].size() << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Recommended Posts:

- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum number of prime factors a number can have with exactly x factors
- 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
- Number which has the maximum number of distinct prime factors in the range M to N
- Distinct Prime Factors of Array Product
- Distinct Prime Factors of an Array
- Maximum distinct prime factors of elements in a K-length subarray
- Count ways to split N! into two distinct co-prime factors
- Sort an array according to the increasing count of distinct Prime Factors
- Find number of factors of N when location of its two factors whose product is N is given
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Number of factors of very large number N modulo M where M is any prime number
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- First N natural can be divided into two sets with given difference and co-prime sums
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.