def mul(x): out = 1 while x != 1 and x != 0: out *= x x -= 1 return out def next(x): out = 0 while x != 0: out += mul(x % 10) x /= 10 return out def make(x): out = [x] while 1: tmp = next(out[-1]) if out.count(tmp): return out out.append(tmp) total = [] bak = [] for i in xrange(1, 2000):#1000001): if bak.count(next(i)): print i, bak total.append([i, next(i)]) continue tmp = make(i) if len(tmp) >= 60: print i, tmp total.append(tmp) bak.append(tmp[1]) print total