Digits Product
Description
Given an integer product
, find the smallest positive (i.e. greater than 0
) integer the product of whose digits is equal to product
. If there is no such integer, return -1
instead.
Example
-
For
product = 12
, the output should be
digitsProduct(product) = 26
; -
For
product = 19
, the output should be
digitsProduct(product) = -1
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] integer product
Guaranteed constraints:
0 ≤ product ≤ 600
.
- [output] integer
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
function digitsProduct(product) {
for(var i = 1; i<5000; i++) {
if(p(i) === product)return i;
}
function p (k) {
return String(k).split('').map(Number).reduce((a,b)=>a*b,1)
}
return -1;
}