Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Digits solution in Clear category for Numbers Factory by PositronicLlama
"""
Return the smallest number such that the products of its digits equal a given number.
"""
import functools
def checkio(number):
"""
Return the smallest integer such that the product of its digits equal 'number'.
If this is not possible, return zero.
"""
# Special case for one to keep the logic simpler (and zero, as an early out).
if number <= 1:
return number
# Factor number into factors in the range [2, 9] (inclusive)
factors = []
for d in reversed(range(2, 10)):
while number > 1 and not number % d:
number //= d
factors.append(d)
# If the number has not been completely factored, it must have prime
# factors larger than seven. In this case, it's impossible to factor
# with digits, so return zero.
if number > 1:
return 0
# Combine the factors back into a single integer.
# Use reversed so that smaller digits come first.
return functools.reduce(lambda a, b: 10 * a + b, reversed(factors))
if __name__ == '__main__':
assert checkio(20) == 45, "1st example"
assert checkio(21) == 37, "2nd example"
assert checkio(17) == 0, "3rd example"
assert checkio(33) == 0, "4th example"
assert checkio(5) == 5, "5th example"
July 9, 2013
Comments: