#!/usr/bin/env python import time from decimal import Decimal def mul(s): m = Decimal('1') for c in s: try: m *= Decimal(c) except: print('err s={} c={} m={}'.format(s, c, m)) return str(m) dic = {} def count(s, c=0): if len(s) < 2: return (s, c) if '0' in s: return ('0', c+1) if '1' in s: s = ''.join( filter( lambda c: c!='1', s ) ) if s == '': return ('1', c+1) return count(s, c) s = ''.join( sorted(s) ) if s in dic: (s, c2) = dic.get(s) return (s, c+c2) (s2, c2) = count( mul(s), c+1 ) if c == 0 and s not in dic: dic[s] = (s2, c2) return (s2, c2) judge = lambda s, mc, r, c: (c > mc, s, r, max(c, mc)) calc = lambda s, mc: judge(s, mc, *count(s)) def next_str(s): ck = lambda t, s: any( map( lambda c: c in s, t ) ) def up(i): if i < -len(s): return '2'* (len(s) + 1) if s[i] != '9': c = str(int(s[i]) + 1) return s[:i] + c * (-i) return up(i-1) while True: s = up(-1) if ck('01', s): continue if '5' in s and ck('2468', s): continue break return s if __name__ == "__main__": st = time.time() (s, c) = ('0', 0) while True: (f, s, r, c) = calc( next_str(s), c ) if f: tm = time.time() - st print('{}, {}, {} # {} sec'.format(s, r, c, tm)) # EOF