Find if n can be written as product of k numbers
Last Updated :
15 Jul, 2022
Given a positive number n, we need to print exactly k positive numbers (all greater than 1) such that product of those k numbers is n. If there doesn’t exist such k numbers, print -1 . If there are many possible answer you have to print one of that answer where k numbers are sorted.
Examples:
Input : n = 54, k = 3
Output : 2, 3, 9
Note that 2, 3 and 9 are k numbers
with product equals to n.
Input : n = 54, k = 8
Output : -1
This problem uses idea very similar to print all prime factors of a given number.
The idea is very simple. First we calculate all prime factors of n and store them in a vector. Note we store each prime number as many times as it appears in it’s prime factorization. Now to find k numbers greater than 1, we check if size of our vector is greater than or equal to k or not.
- If size is less than k we print -1.
- Else we print first k-1 factors as it is from vector and last factor is product of all the remaining elements of vector.
Note we inserted all the prime factors in sorted manner hence all our number in vector are sorted. This also satisfy our sorted condition for k numbers.
C++
#include <bits/stdc++.h>
using namespace std;
void kFactors( int n, int k)
{
vector< int > P;
while (n%2 == 0)
{
P.push_back(2);
n /= 2;
}
for ( int i=3; i*i<=n; i=i+2)
{
while (n%i == 0)
{
n = n/i;
P.push_back(i);
}
}
if (n > 2)
P.push_back(n);
if (P.size() < k)
{
cout << "-1" << endl;
return ;
}
for ( int i=0; i<k-1; i++)
cout << P[i] << ", " ;
int product = 1;
for ( int i=k-1; i<P.size(); i++)
product = product*P[i];
cout << product << endl;
}
int main()
{
int n = 54, k = 3;
kFactors(n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void kFactors( int n, int k)
{
ArrayList<Integer> P = new ArrayList<Integer>();
while (n % 2 == 0 )
{
P.add( 2 );
n /= 2 ;
}
for ( int i = 3 ; i * i <= n; i = i + 2 )
{
while (n % i == 0 )
{
n = n / i;
P.add(i);
}
}
if (n > 2 )
P.add(n);
if (P.size() < k)
{
System.out.println( "-1" );
return ;
}
for ( int i = 0 ; i < k - 1 ; i++)
System.out.print(P.get(i) + ", " );
int product = 1 ;
for ( int i = k - 1 ; i < P.size(); i++)
product = product * P.get(i);
System.out.println(product);
}
public static void main(String[] args)
{
int n = 54 , k = 3 ;
kFactors(n, k);
}
}
|
Python3
import math as mt
def kFactors(n, k):
a = list ()
while n % 2 = = 0 :
a.append( 2 )
n = n / / 2
for i in range ( 3 , mt.ceil(mt.sqrt(n)), 2 ):
while n % i = = 0 :
n = n / i;
a.append(i)
if n > 2 :
a.append(n)
if len (a) < k:
print ( "-1" )
return
for i in range (k - 1 ):
print (a[i], end = ", " )
product = 1
for i in range (k - 1 , len (a)):
product * = a[i]
print (product)
n, k = 54 , 3
kFactors(n, k)
|
C#
using System;
using System.Collections;
class GFG
{
static void kFactors( int n, int k)
{
ArrayList P = new ArrayList();
while (n % 2 == 0)
{
P.Add(2);
n /= 2;
}
for ( int i = 3; i * i <= n; i = i + 2)
{
while (n % i == 0)
{
n = n / i;
P.Add(i);
}
}
if (n > 2)
P.Add(n);
if (P.Count < k)
{
Console.WriteLine( "-1" );
return ;
}
for ( int i = 0; i < k - 1; i++)
Console.Write(P[i]+ ", " );
int product = 1;
for ( int i = k - 1; i < P.Count; i++)
product = product*( int )P[i];
Console.WriteLine(product);
}
static void Main()
{
int n = 54, k = 3;
kFactors(n, k);
}
}
|
PHP
<?php
function kFactors( $n , $k )
{
$P = array ();
while ( $n % 2 == 0)
{
array_push ( $P , 2);
$n = (int)( $n / 2);
}
for ( $i = 3; $i * $i <= $n ; $i = $i + 2)
{
while ( $n % $i == 0)
{
$n = (int)( $n / $i );
array_push ( $P , $i );
}
}
if ( $n > 2)
array_push ( $P , $n );
if ( count ( $P ) < $k )
{
echo "-1\n" ;
return ;
}
for ( $i = 0; $i < $k - 1; $i ++)
echo $P [ $i ] . ", " ;
$product = 1;
for ( $i = $k - 1; $i < count ( $P ); $i ++)
$product = $product * $P [ $i ];
echo $product ;
}
$n = 54;
$k = 3;
kFactors( $n , $k );
?>
|
Javascript
<script>
function kFactors(n , k) {
var P = Array();
while (n % 2 == 0) {
P.push(2);
n = parseInt(n/2);
}
for (i = 3; i * i <= n; i = i + 2) {
while (n % i == 0) {
n = parseInt(n/i);
P.push(i);
}
}
if (n > 2)
P.push(n);
if (P.length < k) {
document.write( "-1" );
return ;
}
for (i = 0; i < k - 1; i++)
document.write(P[i] + ", " );
var product = 1;
for (i = k - 1; i < P.length; i++)
product = product * P[i];
document.write(product);
}
var n = 54, k = 3;
kFactors(n, k);
</script>
|
Output:
2, 3, 9
Time Complexity: O(?n log n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...