Find the smallest number whose digits multiply to a given number n
2014-07-16 05:49
579 查看
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:
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.
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 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.
public static int findSmallest(int n){ if(n < 10) return 10 + n; int out = 0; int pos = 0; for(int i = 9; i > 1; i --){ while(n % i == 0){ n /= i; out += i * Math.pow(10, pos); pos ++; } } return n ==1 ? out : -1; }
相关文章推荐
- 【Leetcode】Given a binary tree, find all root-to-leaf paths whose sum equals a given number
- Given a number,find the next higher number using the same digits in the number.
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- Google Treasure Hunt 2008---Find the Smallest Prime Number
- You’re given an array containing both positive and negative integers and required to find the sub-a
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- quick select to find the kth smallest element in array
- Unable to find a team with the given Team ID 问题解决方法
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- given two directory trees how to find which files are the same?
- Google Treasure Hunt 2008---Find the Smallest Prime Number
- [Dynamic Programming]Recursion/Non-recursion to find the max number
- Unable to find a team with the given Team ID或者Failed to code sign的问题解决
- Given the numbers 1 to 1000, what is the minimum number?
- In a bunch of number to find out the biggest minimum value
- CareerCup Find the smallest range that includes at least one number from each of the k sorted lists.
- Find a number in the array having least difference with the given number (n)
- Find the smallest positive number missing from an unsorted array