×   Home   Blog   Newsletter   Privacy   Contact Us   About

Recovering Source Code Puzzle

A little math puzzle this week.
On an old computer system, I have a program, and part of of this program is a function called FooBar. The softcopy of the source code is missing, but I do have a hardcopy printout (shown below). Unfortunately, this printout has been damaged by water and the three important constants have been obscured.
I do, however, have a compiled version of the function and a test frame that allows me to input the three parameters: x,y,z and display the output of the function. I know that, for the language on this system, all variables are Natural Numbers of arbitrary length (that is, they are integers greater than, or equal to, zero).
The function is pretty simple, it takes three parameters, multiplies each with a distinct constant and outputs the sum of the these products. Your task is to recover the three constants A,B,C with the smallest number of calls to the function.
Advertisement:

Three Variables, three unknowns

This does not seem that hard a problem !?!? We have three unknowns, so all we need is three (distinct) sets of input/output and we can solve the simultaneous equations. In fact, if we want to be be math lazy, since we can control the inputs, we can do something as simple as this to devine the constants:

print FooBar(1,0,0);     //returns A
print FooBar(0,1,0);     //returns B
print FooBar(0,0,1);     //returns C
But this is inefficient. We can actually do it with just two calls to the function. Huh? how is this possible? What Black Magic is this that allows you to determine three unknowns with just two answers? The secret is that we can leverage information gained from the first call of the function to adjust the parameters we call it with for the second time.
How can we do this with just two calls to the function?

Solution

Need some hints? Click the buttons below.