Problem 172610 · hard · Level 01 Prerequisites & Setup

The Puzzle Box Code

functions · factorisation · digits · greedy choice

The lid of a puzzle box is engraved with a number p. The code that opens it is the smallest positive integer whose digits multiply to exactly p. For example, the digits of 49 multiply to 4 * 9 = 36, and no smaller positive integer does, so for p = 36 the code is 49.

Write box_code(p) that returns the code as an integer, or -1 if no positive integer has digits whose product is p.

Examples

Input:  p = 36
Output: 49

Input:  p = 100
Output: 455

Input:  p = 13
Output: -1

Input:  p = 0
Output: 10

Constraints

  • 0 <= p <= 10**18

Goals

  • Break a number into single-digit factors
  • Argue why a greedy order gives the smallest number
  • Treat 0 and 1 as special cases
Starting Python…