f = lambda x: 3 * x / 7 gcd = lambda x, y: (y == 0) and x or gcd(y, x % y) maxx = [1] * 3 for i in xrange(1, 1000001): if i % 7 == 0: continue tmpi = f(i) if gcd(i, tmpi) == 1: tmp = 3.0 / 7 - float(tmpi) / i if tmp < maxx[0]: maxx = [tmp, tmpi, i] print maxx