def gcd(a, b): return 0 == b and a or gcd(b, a % b) def t(limit, a, b): l = [] for m in range(limit + 1, 1, -1): n, d = divmod(m * a, b) if gcd(n, m) == 1: l.append((d / m, n, m)) if 1 == d: break for x in sorted(l, key=lambda x: x[0]): return x[1:] print(t(1000000, 3, 7))