Skip to content

Digits Product

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

    digits_product(product) = 26
  • For product = 19, the output should be

    digits_product(product) = -1

Input/Output

  • [input] integer product

    Guaranteed constraints:0 ≤ product ≤ 600.

Solution

py
def digits_product(product):
    # #smallest possible number for product 400 is 3558
    for i in range(1, 3559):
        smallest = 1
        for char in str(i):
            smallest *= int(char)
        if(smallest == product):
            return i
    return -1


print(digits_product(600))
js
// def digits_product(product):
//     # #smallest possible number for product 400 is 3558
//     for i in range(1, 3559):
//         smallest = 1
//         for char in str(i):
//             smallest *= int(char)
//         if(smallest == product):
//             return i
//     return -1

// print(digits_product(600))

function digitsProduct(product) {
  // smallest possible number for product 400 is 3558
  for (let i = 1; i < 3559; i++) {
    let smallest = 1;
    for (let char of i.toString()) {
      smallest *= parseInt(char);
    }
    if (smallest === product) {
      return i;
    }
  }
  return -1;
}

console.log(digitsProduct(600));

my thoughts are neither my employer's nor my wife's