Given a number N, print all its unique prime factors and their powers in N.
Examples:
Input: N = 100
Output: Factor Power
2 2
5 2
Input: N = 35
Output: Factor Power
5 1
7 1
A Simple Solution is to first find prime factors of N. Then for every prime factor, find the highest power of it that divides N and print it.
An Efficient Solution is to use Sieve of Eratosthenes.
1) First compute an array s[N+1] using Sieve of Eratosthenes.
s[i] = Smallest prime factor of "i" that
divides "i".
For example let N = 10
s[2] = s[4] = s[6] = s[8] = s[10] = 2;
s[3] = s[9] = 3;
s[5] = 5;
s[7] = 7;
2) Using the above computed array s[],
we can find all powers in O(Log N) time.
curr = s[N]; // Current prime factor of N
cnt = 1; // Power of current prime factor
// Printing prime factors and their powers
while (N > 1)
{
N /= s[N];
// N is now N/s[N]. If new N also has its
// smallest prime factor as curr, increment
// power and continue
if (curr == s[N])
{
cnt++;
continue;
}
// Print prime factor and its power
print(curr, cnt);
// Update current prime factor as s[N] and
// initializing count as 1.
curr = s[N];
cnt = 1;
}
Below is the implementation of above steps.
C++
#include<bits/stdc++.h>
using namespace std;
void sieveOfEratosthenes( int N, int s[])
{
vector < bool > prime(N+1, false );
for ( int i=2; i<=N; i+=2)
s[i] = 2;
for ( int i=3; i<=N; i+=2)
{
if (prime[i] == false )
{
s[i] = i;
for ( int j=i; j*i<=N; j+=2)
{
if (prime[i*j] == false )
{
prime[i*j] = true ;
s[i*j] = i;
}
}
}
}
}
void generatePrimeFactors( int N)
{
int s[N+1];
sieveOfEratosthenes(N, s);
printf ( "Factor Power\n" );
int curr = s[N];
int cnt = 1;
while (N > 1)
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
printf ( "%d\t%d\n" , curr, cnt);
curr = s[N];
cnt = 1;
}
}
int main()
{
int N = 360;
generatePrimeFactors(N);
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static void sieveOfEratosthenes( int N,
int s[])
{
boolean [] prime = new boolean [N + 1 ];
for ( int i = 2 ; i <= N; i += 2 )
s[i] = 2 ;
for ( int i = 3 ; i <= N; i += 2 )
{
if (prime[i] == false )
{
s[i] = i;
for ( int j = i; j * i <= N; j += 2 )
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static void generatePrimeFactors( int N)
{
int [] s = new int [N + 1 ];
sieveOfEratosthenes(N, s);
System.out.println( "Factor Power" );
int curr = s[N];
int cnt = 1 ;
while (N > 1 )
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
System.out.println(curr + "\t" + cnt);
curr = s[N];
cnt = 1 ;
}
}
public static void main(String[] args)
{
int N = 360 ;
generatePrimeFactors(N);
}
}
|
Python3
def sieveOfEratosthenes(N, s):
prime = [ False ] * (N + 1 )
for i in range ( 2 , N + 1 , 2 ):
s[i] = 2
for i in range ( 3 , N + 1 , 2 ):
if (prime[i] = = False ):
s[i] = i
for j in range (i, int (N / i) + 1 , 2 ):
if (prime[i * j] = = False ):
prime[i * j] = True
s[i * j] = i
def generatePrimeFactors(N):
s = [ 0 ] * (N + 1 )
sieveOfEratosthenes(N, s)
print ( "Factor Power" )
curr = s[N]
cnt = 1
while (N > 1 ):
N / / = s[N]
if (curr = = s[N]):
cnt + = 1
continue
print ( str (curr) + "\t" + str (cnt))
curr = s[N]
cnt = 1
N = 360
generatePrimeFactors(N)
|
C#
class GFG
{
static void sieveOfEratosthenes( int N, int [] s)
{
bool [] prime = new bool [N + 1];
for ( int i = 2; i <= N; i += 2)
s[i] = 2;
for ( int i = 3; i <= N; i += 2)
{
if (prime[i] == false )
{
s[i] = i;
for ( int j = i; j * i <= N; j += 2)
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
static void generatePrimeFactors( int N)
{
int [] s = new int [N + 1];
sieveOfEratosthenes(N, s);
System.Console.WriteLine( "Factor Power" );
int curr = s[N];
int cnt = 1;
while (N > 1)
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
System.Console.WriteLine(curr + "\t" + cnt);
curr = s[N];
cnt = 1;
}
}
static void Main()
{
int N = 360;
generatePrimeFactors(N);
}
}
|
PHP
<?php
function sieveOfEratosthenes( $N , & $s )
{
$prime = array_fill (0, $N + 1, false);
for ( $i = 2; $i <= $N ; $i += 2)
$s [ $i ] = 2;
for ( $i = 3; $i <= $N ; $i += 2)
{
if ( $prime [ $i ] == false)
{
$s [ $i ] = $i ;
for ( $j = $i ; $j * $i <= $N ; $j += 2)
{
if ( $prime [ $i * $j ] == false)
{
$prime [ $i * $j ] = true;
$s [ $i * $j ] = $i ;
}
}
}
}
}
function generatePrimeFactors( $N )
{
$s = array_fill (0, $N + 1, 0);
sieveOfEratosthenes( $N , $s );
print ( "Factor Power\n" );
$curr = $s [ $N ];
$cnt = 1;
while ( $N > 1)
{
if ( $s [ $N ])
$N = (int)( $N / $s [ $N ]);
if ( $curr == $s [ $N ])
{
$cnt ++;
continue ;
}
print ( $curr . "\t" . $cnt . "\n" );
$curr = $s [ $N ];
$cnt = 1;
}
}
$N = 360;
generatePrimeFactors( $N );
?>
|
Javascript
<script>
function sieveOfEratosthenes(N, s)
{
prime = Array.from({length: N+1}, (_, i) => false );
for (i = 2; i <= N; i += 2)
s[i] = 2;
for (i = 3; i <= N; i += 2)
{
if (prime[i] == false )
{
s[i] = i;
for (j = i; j * i <= N; j += 2)
{
if (prime[i * j] == false )
{
prime[i * j] = true ;
s[i * j] = i;
}
}
}
}
}
function generatePrimeFactors(N)
{
var s = Array.from({length: N+1}, (_, i) => 0);
sieveOfEratosthenes(N, s);
document.write( "Factor Power" );
var curr = s[N];
var cnt = 1;
while (N > 1)
{
N /= s[N];
if (curr == s[N])
{
cnt++;
continue ;
}
document.write( "<br>" +curr + "\t" + cnt);
curr = s[N];
cnt = 1;
}
}
var N = 360;
generatePrimeFactors(N);
</script>
|
Output:
Factor Power
2 3
3 2
5 1
Time Complexity: O(n*log(log(n)))
Auxiliary Space: O(n)
The above algorithm finds all powers in O(Log N) time after we have filled s[]. This can be very useful in competitive environment where we have an upper limit and we need to compute prime factors and their powers for many test cases. In this scenario, the array needs to be s[] filled only once.
This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.