// approximsum -- approximate number as sum of integer multiples of two others // David Eppstein, UC Irvine, 18 Jun 1996 // given a,b,c find x,y >= 0 s.t. ax+by is as close as possible to c // breaking ties in favor of ax+by= 0) { long diff = a*i + b*j - c; if (diff > 0) { if (d > diff) { d = diff; x = i; y = j; } i--; } else { if (d >= -diff) { d = -diff; x = i; y = j; } j++; } } }