import math def num_match(num): return sorted(str(num)) == sorted('123456789') def fibnacci_mod(): i, a, b = 0, 0, 1 while True: i, a, b = i + 1, b % 1000000000, a + b yield i, a def head_match(k): exp = k * math.log10((1 + 5 ** 0.5) / 2) - math.log10(5) / 2 n = 10 ** (8 + exp % 1) return num_match(str(n)[:9]) def search(): for k, n in fibnacci_mod(): if num_match(n): if head_match(k): return k, n print(search())