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++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100001
int spf[MAXN];
vector< int >adj[MAXN];
void sieve()
{
spf[1] = 1;
for ( int i=2; i<MAXN; i++)
spf[i] = i;
for ( int i=2; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for ( int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
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];
while (x != 1)
{
if (adj[i][index-1]!=spf[x])
{
adj[i].push_back(spf[x]);
index+=1;
}
x = x / spf[x];
}
}
}
int main()
{
sieve();
int n = 10;
getdistinctFactorization(n);
cout << "Distinct prime factor for first " << n
<< " natural number" << " : " ;
for ( int i=1; i<=n; i++)
cout << adj[i].size() << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int MAXN = 100001 ;
static int [] spf = new int [MAXN];
static ArrayList<ArrayList<Integer>> adj =
new ArrayList<ArrayList<Integer>>();
static void sieve()
{
for ( int i = 0 ; i < MAXN; i++)
{
adj.add( new ArrayList<Integer>());
}
spf[ 1 ] = 1 ;
for ( int i = 2 ; i < MAXN; i++)
{
spf[i] = i;
}
for ( int i = 2 ; i * i < MAXN; i++)
{
if (spf[i] == i)
{
for ( int j = i * i; j < MAXN; j += i)
{
if (spf[j] == j)
spf[j] = i;
}
}
}
}
static void getdistinctFactorization( int n)
{
int index, x, i;
for (i = 1 ; i <= n; i++)
{
index = 1 ;
x = i;
if (x != 1 )
adj.get(i).add(spf[x]);
x = x / spf[x];
while (x != 1 )
{
if (adj.get(i).get(index - 1 ) != spf[x])
{
adj.get(i).add(spf[x]);
index += 1 ;
}
x = x / spf[x];
}
}
}
public static void main (String[] args)
{
sieve();
int n = 10 ;
getdistinctFactorization(n);
System.out.print( "Distinct prime factor for first " +
n + " natural number" + " : " );
for ( int i = 1 ; i <= n; i++)
{
System.out.print(adj.get(i).size()+ " " );
}
}
}
|
Python3
def sieve():
global spf, adj, MAXN
spf[ 1 ] = 1
for i in range ( 2 , MAXN):
spf[i] = i
for i in range ( 2 , MAXN):
if i * i > MAXN:
break
if (spf[i] = = i):
for j in range (i * i, MAXN, i):
if (spf[j] = = j):
spf[j] = i
def getdistinctFactorization(n):
global adj, spf, MAXN
index = 0
for i in range ( 1 , n + 1 ):
index = 1
x = i
if (x ! = 1 ):
adj[i].append(spf[x])
x = x / / spf[x]
while (x ! = 1 ):
if (adj[i][index - 1 ] ! = spf[x]):
adj[i].append(spf[x])
index + = 1
x = x / / spf[x]
if __name__ = = '__main__' :
MAXN = 100001
spf = [ 0 for i in range (MAXN)]
adj = [[] for i in range (MAXN)]
sieve()
n = 10
getdistinctFactorization(n)
print ( "Distinct prime factor for first " , n, " natural number : " , end = "")
for i in range ( 1 , n + 1 ):
print ( len (adj[i]), end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int MAXN = 100001;
static int [] spf = new int [MAXN];
static List<List< int >> adj = new List<List< int >>();
static void sieve()
{
for ( int i = 0; i < MAXN; i++)
{
adj.Add( new List< int >());
}
spf[1] = 1;
for ( int i = 2; i < MAXN; i++)
{
spf[i] = i;
}
for ( int i = 2; i * i < MAXN; i++)
{
if (spf[i] == i)
{
for ( int j = i * i; j < MAXN; j += i)
{
if (spf[j] == j)
spf[j] = i;
}
}
}
}
static void getdistinctFactorization( int n)
{
int index, x, i;
for (i = 1; i <= n; i++)
{
index = 1;
x = i;
if (x != 1)
{
adj[i].Add(spf[x]);
}
x = x / spf[x];
while (x != 1)
{
if (adj[i][index-1] != spf[x])
{
adj[i].Add(spf[x]);
index += 1;
}
x = x / spf[x];
}
}
}
static public void Main ()
{
sieve();
int n = 10;
getdistinctFactorization(n);
Console.Write( "Distinct prime factor for first " +
n + " natural number" + " : " );
for ( int i = 1; i <= n; i++)
{
Console.Write(adj[i].Count + " " );
}
}
}
|
Javascript
<script>
let MAXN = 100001;
let spf = new Array(MAXN);
let adj=[];
function sieve()
{
for (let i = 0; i < MAXN; i++)
{
adj.push([]);
}
spf[1] = 1;
for (let i = 2; i < MAXN; i++)
{
spf[i] = i;
}
for (let i = 2; i * i < MAXN; i++)
{
if (spf[i] == i)
{
for (let j = i * i; j < MAXN; j += i)
{
if (spf[j] == j)
spf[j] = i;
}
}
}
}
function getdistinctFactorization(n)
{
let index, x, i;
for (i = 1; i <= n; i++)
{
index = 1;
x = i;
if (x != 1)
adj[i].push(spf[x]);
x = Math.floor(x / spf[x]);
while (x != 1)
{
if (adj[i][index - 1] != spf[x])
{
adj[i].push(spf[x]);
index += 1;
}
x = Math.floor(x / spf[x]);
}
}
}
sieve();
let n = 10;
getdistinctFactorization(n);
document.write( "Distinct prime factor for first " +
n + " natural number" + " : " );
for (let i = 1; i <= n; i++)
{
document.write(adj[i].length+ " " );
}
</script>
|
OutputDistinct prime factor for first 10 natural number : 0 1 1 1 1 2 1 1 1 2