Skip to content

Instantly share code, notes, and snippets.

@methane
Created April 27, 2011 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/943827 to your computer and use it in GitHub Desktop.
Save methane/943827 to your computer and use it in GitHub Desktop.
X = 233
Y = 456
Z = 787
TARGET = 10000000000
def gcd(x, y): # x >= y
mod = x%y
if mod == 0: return y
return gcd(y, x%y)
def lcm(x, y):
g = gcd(max(x,y), min(x,y))
return x*y / g
MAX_X = min(lcm(Y, X)/X, lcm(Z, X)/X)
MAX_Y = lcm(Y, Z)/Y
def solve():
tgt = TARGET
_X = X
_Y = Y
_Z = Z
min_result = tgt
for num_xy in xrange(MAX_X + MAX_Y - 1):
for x in xrange(num_xy):
y = num_xy - x
if (tgt - y*_Y - x*_X) % Z == 0:
z = (tgt - y*_Y - x*_X) / Z
result = x+y+z
if result < min_result:
print x, y, z, x*_X+y*_Y+z*_Z
min_result = result
break
return min_result
print solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment