number puzzle: identify two numbers using sums of operations on the numbers

76 Views Asked by At

I have a number puzzle:

You have two unknown positive integers, lets call them $a$ and $b$, that are not equal.

You can perform simple mathematical operations on the individual numbers (lets say +, -, /, *, ^, modulus, bit-wise operations) and you can only work with integers, no decimals. But you can only know the sum of the results. So if $f$ is a function of a single integer ($f$ can contain multiple of the operations above and any other known integers you want):

$y = f(a) + f(b)$

You can only know the value of $y$.

The goal is to identify $a$ and $b$ (you dont need to distinguish between them). You can perform as many of these operations as you want, the goal is to use as few as possible. I don't know whether or not this is possible, but it seems like it should be...

EDIT: There has been some confusion so let me try to clarify with an example (taken from Sridhar Ramesh's answer):

first we could choose $f(x) = x$ which gives us:

$y_1 = a + b$

second we could choose $f(x) = x^2$ which gives us:

$y_2 = a^2 + b^2$

See that each $f$ is a function of exactly one of our unknown integers.

Now, given the value of $y_1$ and $y_2$, determine the values of $a$ and $b$.

2

There are 2 best solutions below

6
On BEST ANSWER

You cannot distinguish which is $a$ and which $b$ but

$$f(a)=2^a$$ works: write $f(a)+f(b)$ in binary and read the positions of the two $1$s as the corresponding powers of $2$. This also works if $a$ or $b$ can be $0$ or can be equal

2
On

Here's a very nice, very general idea:

Calculate $a + b$ (that is, the sum of the identity function on the two values) and $a^2 + b^2$ (that is, the sum of the squaring function on the two values).

From these, you can then compute $(a + b)^2 - (a^2 + b^2) = 2ab$, and thus compute $ab$. Now you know both $a + b$ and $ab$, from which finding the two values is simply solving a quadratic equation. I.e., you should now factor the quadratic polynomial $X^2 + (a + b)X + ab$ (using the quadratic formula, if you like), recovering the two factors $(X + a)$ and $(X + b)$, telling you your two values.

If you want an explicit formula, let $s = a + b$ and let $n = a^2 + b^2$, and note that $a$ and $b$ are given by $(s/2) \pm \sqrt{n/2 -(s/2)^2}$.

This works much more generally than over positive integers, and also works even if the two values are the same. In fact, a variant of this works for multisets of n many values, using sums of up through nth powers.