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.
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 n, the output is always n+10. For example for n = 7, 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 result are 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.
17 49 Not possible 455
This article is contributed by Ashish Bansal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Smallest number k such that the product of digits of k is equal to n
- Greedy Algorithm for Egyptian Fraction
- How to check if an instance of 8 puzzle is solvable?
- Birthday Paradox
- Multiply two polynomials
- Count ways to reach the n'th stair
- Find next greater number with same set of digits
- Calculate the angle between hour hand and minute hand
- Write a function that generates one of 3 numbers according to given probabilities
- Count trailing zeroes in factorial of a number
- Efficient program to print all prime factors of a given number
- Generate integer from 1 to 7 with equal probability
- Sieve of Eratosthenes
- The Knight's tour problem | Backtracking-1
- Write a program to print all permutations of a given string