Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Finding factors in decreasing order solution in Clear category for Numbers Factory by H0r4c3
from math import prod
def factors(n):
facs = list()
c_list = [9, 8, 7, 6, 5, 4, 3, 2]
i = 0
while (n > 1):
c = c_list[i]
if (n % c == 0):
facs.append(c)
n = n / c
else:
i += 1
if i == 8:
return sorted(facs)
return sorted(facs)
def convert_into_integer(facs):
facs_str = ''.join([str(item) for item in facs])
facs_int = int(facs_str)
return facs_int
def checkio(number):
facs = factors(number)
if len(facs) < 2 or prod(facs) < number:
return 0
facs_int = convert_into_integer(facs)
return facs_int
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
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(3125) == 55555, "5th example"
assert checkio(9973) == 0, "6th example"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Jan. 7, 2023