1 Answer | Add Yours
This exercise is designed to make you think in an original manner about algorithms. One possible solution is to first multiply both numbers by -1, and then subtract one number from the next number, and then multiply the result by -1 to convert the result back to a positive number. Thus, if the two numbers were X and Y, the result Z would be obtained by calculating:
Z = -1 * ((-1 * X) – (-1 * Y))
Although there are few cases where this specific algorithm would be useful, the type of thinking involved is really important for real time and machine language programming where you can often save machine cycles by forcing specific operations (e.g. shifting left rather than multiplying by 2).
We’ve answered 315,564 questions. We can answer yours, too.Ask a question