Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Numbers Factory by Moff
import math
from functools import reduce
def factorize(n):
if n < 2:
return []
res = []
# iterate over all even numbers first.
while n % 2 == 0:
res.append(2)
n //= 2
# try odd numbers up to sqrt(n)
limit = math.sqrt(n + 1)
i = 3
while i <= limit:
if n % i == 0:
res.append(i)
n //= i
limit = math.sqrt(n + i)
else:
i += 2
if n != 1:
res.append(n)
p, pw = res[0], 1
result = []
for n in res[1:]:
if n == p:
pw += 1
else:
result.append((p, pw))
p, pw = n, 1
result.append((p, pw))
return result
def checkio(n):
factors = factorize(n)
if any(p > 9 for p, k in factors):
return 0
f = ''.join(str(p) * k for p, k in factors)
for s, t in (('222', '8'), ('33', '9'), ('23', '6'), ('22', '4')):
f = ''.join(sorted(f.replace(s, t)))
return int(''.join(sorted(f)))
Aug. 1, 2015