def gen(): a = 1 # for all b = 1 # for blue while True: yield (a + 1) // 2, (b + 1) // 2 a, b = 3 * a + 4 * b, 2 * a + 3 * b def search(limit): for a, b in gen(): if a > limit: return b print(search(int(10 ** 12)))