Number of steps to convert to prime factors
Last Updated :
18 Sep, 2023
Given an array arr[] of n positive integers. Represent every number as its factors (x * y = arr[i]) [Here x or y cannot be 1] till it cannot be further represented as x*y = arr[i]. Print the number of steps required to split it till no further representations are possible.
Examples:
Input: 4 4 4
Output: 3
Explanation: 1st step 2 2 4 4 .
2nd step 2 2 2 2 4
3rd step 2 2 2 2 2 2
Input: 20 4
Output: 3
Explanation: 1st step 20 2 2 (2*2 = 4)
2nd step 2 10 2 2 (2*10 = 20)
3rd step 2 2 5 2 2, (2*5 = 10)
Approach is to pre calculate the prime factors of every number. Prime factors can be efficiently calculated using Sieve’s implementation which requires N * log N. . We know a number can be represented as a multiplication of its prime factors and we can represent it till its not 1, so 1 is not taken into count, so if the number is other than 1, we count the number of prime factors, and then subtract 1 from it.
Implementation:
C++
#include <iostream>
using namespace std;
const int MAX = 1000001;
int factor[MAX] = { 0 };
void cal_factor()
{
factor[1] = 1;
for ( int i = 2; i < MAX; i++)
factor[i] = i;
for ( int i = 4; i < MAX; i += 2)
factor[i] = 2;
for ( int i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
int no_of_representations( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
int temp = a[i];
int flag = 0;
while (factor[temp] != 1) {
flag = -1;
count++;
temp = temp / factor[temp];
}
count += flag;
}
return count;
}
int main()
{
cal_factor();
int a[] = { 4, 4, 4 };
int n = sizeof (a) / sizeof (a[0]);
cout << no_of_representations(a, n);
return 0;
}
|
Java
class GFG
{
static final int MAX = 1000001 ;
static int factor[] = new int [MAX];
static void cal_factor() {
factor[ 1 ] = 1 ;
for ( int i = 2 ; i < MAX; i++)
factor[i] = i;
for ( int i = 4 ; i < MAX; i += 2 )
factor[i] = 2 ;
for ( int i = 3 ; i * i < MAX; i++) {
if (factor[i] == i) {
for ( int j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
static int no_of_representations( int a[], int n) {
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int temp = a[i];
int flag = 0 ;
while (factor[temp] != 1 ) {
flag = - 1 ;
count++;
temp = temp / factor[temp];
}
count += flag;
}
return count;
}
public static void main(String[] args) {
cal_factor();
int a[] = { 4 , 4 , 4 };
int n = a.length;
System.out.print(no_of_representations(a, n));
}
}
|
Python3
MAX = 1000001
factor = [ 0 ] * MAX
def cal_factor():
factor[ 1 ] = 1
for i in range ( 2 , MAX ):
factor[i] = i
for i in range ( 4 , MAX , 2 ):
factor[i] = 2
i = 3
while i * i < MAX :
if (factor[i] = = i) :
for j in range (i * i, MAX , i) :
if (factor[j] = = j):
factor[j] = i
i + = 1
def no_of_representations(a, n):
count = 0
for i in range (n) :
temp = a[i]
flag = 0
while (factor[temp] ! = 1 ) :
flag = - 1
count + = 1
temp = temp / / factor[temp]
count + = flag
return count
if __name__ = = "__main__" :
cal_factor()
a = [ 4 , 4 , 4 ]
n = len (a)
print (no_of_representations(a, n))
|
C#
using System;
class GFG {
static int MAX = 1000001;
static int []factor = new int [MAX];
static void cal_factor()
{
factor[1] = 1;
for ( int i = 2; i < MAX; i++)
factor[i] = i;
for ( int i = 4; i < MAX; i += 2)
factor[i] = 2;
for ( int i = 3; i * i < MAX; i++)
{
if (factor[i] == i)
{
for ( int j = i * i;
j < MAX; j += i)
{
if (factor[j] == j)
factor[j] = i;
}
}
}
}
static int no_of_representations(
int []a, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int temp = a[i];
int flag = 0;
while (factor[temp] != 1)
{
flag = -1;
count++;
temp = temp / factor[temp];
}
count += flag;
}
return count;
}
public static void Main()
{
cal_factor();
int []a = {4, 4, 4};
int n = a.Length;
Console.WriteLine(
no_of_representations(a, n));
}
}
|
PHP
<?php
$MAX = 100001;
$factor = array_fill (0, $MAX + 1, 0);
function cal_factor()
{
global $factor , $MAX ;
$factor [1] = 1;
for ( $i = 2; $i < $MAX ; $i ++)
$factor [ $i ] = $i ;
for ( $i = 4; $i < $MAX ; $i += 2)
$factor [ $i ] = 2;
for ( $i = 3; $i * $i < $MAX ; $i ++)
{
if ( $factor [ $i ] == $i )
{
for ( $j = $i * $i ; $j < $MAX ; $j += $i )
{
if ( $factor [ $j ] == $j )
$factor [ $j ] = $i ;
}
}
}
}
function no_of_representations( $a , $n )
{
global $factor , $MAX ;
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$temp = $a [ $i ];
$flag = 0;
while ( $factor [ $temp ] != 1) {
$flag = -1;
$count ++;
$temp = (int)( $temp / $factor [ $temp ]);
}
$count += $flag ;
}
return $count ;
}
cal_factor();
$a = array ( 4, 4, 4 );
$n = count ( $a );
echo no_of_representations( $a , $n );
?>
|
Javascript
<script>
let MAX = 1000001;
let factor = [];
function cal_factor() {
factor[1] = 1;
for (let i = 2; i < MAX; i++)
factor[i] = i;
for (let i = 4; i < MAX; i += 2)
factor[i] = 2;
for (let i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for (let j = i * i; j < MAX; j += i) {
if (factor[j] == j)
factor[j] = i;
}
}
}
}
function no_of_representations(a, n) {
let count = 0;
for (let i = 0; i < n; i++) {
let temp = a[i];
let flag = 0;
while (factor[temp] != 1) {
flag = -1;
count++;
temp = temp / factor[temp];
}
count += flag;
}
return count;
}
cal_factor();
let a = [4, 4, 4];
let n = a.length;
document.write(no_of_representations(a, n));
</script>
|
Time Complexity: O(N*logN), as the first two for loops we are using will cost us O(Max), then we are using nested loops in cal_factor function which will cost use O(Max*sqrt(Max)) and we are using nested loops in number_of_representations function where outer for loop will traverse N times and inner while loop will traverse logN time and each time we are decrementing by factor division.
Auxiliary Space: O(1000001), as we are using extra space for factor.
Share your thoughts in the comments
Please Login to comment...