import time from functools import reduce from tools import algebra def jump(num): if 9 > num % 10: return 1 else: num += 1 offset = 0 while not num % 10: num //= 10 offset += 1 return 1 + (10 ** offset - 1) // 9 * (num % 10) def gen_pick(digit): maxi = 10 ** (digit + 1) - 1 num = maxi // 9 while num < maxi: yield num num += jump(num) yield maxi def judge(num): while True: a = 0 while num != 0: a += (num % 10) ** 2 num //= 10 if a == 1: return False elif a == 89: return True else: num = a def count(limit, s_num): total = 1 for i in range(len(s_num)): total *= (limit - i) for ch in set(list(s_num)): total //= algebra.factorial(s_num.count(ch)) return total def statistic(limit): total = 0 for digit in range(limit): for num in gen_pick(digit): if judge(num): total += count(limit, str(num)) return total print(statistic(7)) print(time.process_time())