Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ‘n’. The result ‘p’ should have minimum two digits.
Examples:
Input: n = 36
Output: p = 49
// Note that 4*9 = 36 and 49 is the smallest such number
Input: n = 100
Output: p = 455
// Note that 4*5*5 = 100 and 455 is the smallest such number
Input: n = 1
Output:p = 11
// Note that 1*1 = 1
Input: n = 13
Output: Not Possible
For a given n, following are the two cases to be considered.
Case 1: n < 10 When n is smaller than 10, the output is always n+10. For example for n = 7, the output is 17. For n = 9, output is 19.
Case 2: n >= 10 Find all factors of n which are between 2 and 9 (both inclusive). The idea is to start searching from 9 so that the number of digits in the result is minimized. For example, 9 is preferred over 33 and 8 is preferred over 24.
Store all found factors in an array. The array would contain digits in non-increasing order, so finally print the array in reverse order.
Following is the implementation of above concept.
C++
#include<bits/stdc++.h>
using namespace std;
#define MAX 50
void findSmallest( int n)
{
int i, j = 0;
int res[MAX];
if (n < 10)
{
cout << n + 10;
return ;
}
for (i = 9; i > 1; i--)
{
while (n % i == 0)
{
n = n / i;
res[j] = i;
j++;
}
}
if (n > 10)
{
cout << "Not possible" ;
return ;
}
for (i = j - 1; i >= 0; i--)
cout << res[i];
}
int main()
{
findSmallest(7);
cout << "\n" ;
findSmallest(36);
cout << "\n" ;
findSmallest(13);
cout << "\n" ;
findSmallest(100);
return 0;
}
|
C
#include<stdio.h>
#define MAX 50
void findSmallest( int n)
{
int i, j=0;
int res[MAX];
if (n < 10)
{
printf ( "%d" , n+10);
return ;
}
for (i=9; i>1; i--)
{
while (n%i == 0)
{
n = n/i;
res[j] = i;
j++;
}
}
if (n > 10)
{
printf ( "Not possible" );
return ;
}
for (i=j-1; i>=0; i--)
printf ( "%d" , res[i]);
}
int main()
{
findSmallest(7);
printf ( "\n" );
findSmallest(36);
printf ( "\n" );
findSmallest(13);
printf ( "\n" );
findSmallest(100);
return 0;
}
|
Java
import java.io.*;
class Smallest
{
static void findSmallest( int n)
{
int i, j= 0 ;
int MAX = 50 ;
int [] res = new int [MAX];
if (n < 10 )
{
System.out.println(n+ 10 );
return ;
}
for (i= 9 ; i> 1 ; i--)
{
while (n%i == 0 )
{
n = n/i;
res[j] = i;
j++;
}
}
if (n > 10 )
{
System.out.println( "Not possible" );
return ;
}
for (i=j- 1 ; i>= 0 ; i--)
System.out.print(res[i]);
System.out.println();
}
public static void main (String[] args)
{
findSmallest( 7 );
findSmallest( 36 );
findSmallest( 13 );
findSmallest( 100 );
}
}
|
Python3
def findSmallest(n):
if n < 10 :
print (n + 10 )
return
res = []
for i in range ( 9 , 1 , - 1 ):
while n % i = = 0 :
n = n / i
res.append(i)
if n > 10 :
print ( "Not Possible" )
return
n = res[ len (res) - 1 ]
for i in range ( len (res) - 2 , - 1 , - 1 ):
n = 10 * n + res[i]
print (n)
findSmallest( 7 )
findSmallest( 36 )
findSmallest( 13 )
findSmallest( 100 )
|
C#
using System;
class GFG {
static void findSmallest( int n)
{
int i, j=0;
int MAX = 50;
int []res = new int [MAX];
if (n < 10)
{
Console.WriteLine(n + 10);
return ;
}
for (i = 9; i > 1; i--)
{
while (n % i == 0)
{
n = n / i;
res[j] = i;
j++;
}
}
if (n > 10)
{
Console.WriteLine( "Not possible" );
return ;
}
for (i = j-1; i >= 0; i--)
Console.Write(res[i]);
Console.WriteLine();
}
public static void Main ()
{
findSmallest(7);
findSmallest(36);
findSmallest(13);
findSmallest(100);
}
}
|
PHP
<?php
function findSmallest( $n )
{
$i ;
$j = 0;
$res ;
if ( $n < 10)
{
echo $n + 10;
return ;
}
for ( $i = 9; $i > 1; $i --)
{
while ( $n % $i == 0)
{
$n = $n / $i ;
$res [ $j ] = $i ;
$j ++;
}
}
if ( $n > 10)
{
echo "Not possible" ;
return ;
}
for ( $i = $j - 1; $i >= 0; $i --)
echo $res [ $i ];
}
findSmallest(7);
echo "\n" ;
findSmallest(36);
echo "\n" ;
findSmallest(13);
echo "\n" ;
findSmallest(100);
?>
|
Javascript
<script>
function findSmallest(n)
{
let i, j = 0;
let res = new Array(50);
if (n < 10)
{
document.write(n + 10);
return ;
}
for (i = 9; i > 1; i--)
{
while (n % i == 0)
{
n = Math.floor(n / i);
res[j] = i;
j++;
}
}
if (n > 10)
{
document.write( "Not possible" );
return ;
}
for (i = j - 1; i >= 0; i--)
document.write(res[i]);
}
findSmallest(7);
document.write( "<br>" );
findSmallest(36);
document.write( "<br>" );
findSmallest(13);
document.write( "<br>" );
findSmallest(100);
</script>
|
Output:
17
49
Not possible
455
Time Complexity: O(log2n * 10)
Auxiliary Space: O(MAX)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Jan, 2022
Like Article
Save Article