Created
April 27, 2011 06:59
-
-
Save methane/943827 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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