Increasing the coefficients of two numbers to sum to a targeted value I've naively tried to gradually increase two values so eventually, they sum to a targeted value.

Since this code runs many times and eventually for very large values, I don't know how to optimize so that it doesn't take more than 0.5 seconds.

def coefTargetSum(R, X, Y):
    coefResult = []
    for x in range(R):
        for y in range(R):
            if x * X + y * Y == R:
                coefResult.extend([x, y])
                return coefResult
    return -1
Rearrange the equation to find one variable from another.

\$R = xX + yY\$
\$xX = R - yY\$
\$x = \frac{R - yY}{X}\$  

And so, if you don't want negative values of \$x\$, then you need to limit the range to \$\lfloor \frac{R}{X} \rfloor + 1\$.

This also will return floats, which for the most part shouldn't make a difference as it's a subtype of int. However as we're limiting the results to integers then converting the number to an int could make sense.
I also wouldn't return -1, but would instead use None or an empty list. This is as -1 makes no logical sense.
And rather than extending a list with another list, just return the wanted list. Better you could return a tuple.


This allows for:

def coefTargetSum(R, X, Y):
    for y in range(R // X + 1):
        x = (R - y*Y) / X
        if x % 1 == 0:
            return int(x), y
    return None