Designing function f(f(n)) == -n
A question I got on my last interview:
Design a function
f
, such that:f(f(n)) == -n
Where
n
is a 32 bit signed integer; you can't use complex numbers arithmetic.If you can't design such a function for the whole range of numbers, design it for the largest range possible.
Any ideas?
Solution 1:
You didn't say what kind of language they expected... Here's a static solution (Haskell). It's basically messing with the 2 most significant bits:
f :: Int -> Int
f x | (testBit x 30 /= testBit x 31) = negate $ complementBit x 30
| otherwise = complementBit x 30
It's much easier in a dynamic language (Python). Just check if the argument is a number X and return a lambda that returns -X:
def f(x):
if isinstance(x,int):
return (lambda: -x)
else:
return x()
Solution 2:
How about:
f(n) = sign(n) - (-1)n * n
In Python:
def f(n):
if n == 0: return 0
if n >= 0:
if n % 2 == 1:
return n + 1
else:
return -1 * (n - 1)
else:
if n % 2 == 1:
return n - 1
else:
return -1 * (n + 1)
Python automatically promotes integers to arbitrary length longs. In other languages the largest positive integer will overflow, so it will work for all integers except that one.
To make it work for real numbers you need to replace the n in (-1)n with { ceiling(n) if n>0; floor(n) if n<0 }
.
In C# (works for any double, except in overflow situations):
static double F(double n)
{
if (n == 0) return 0;
if (n < 0)
return ((long)Math.Ceiling(n) % 2 == 0) ? (n + 1) : (-1 * (n - 1));
else
return ((long)Math.Floor(n) % 2 == 0) ? (n - 1) : (-1 * (n + 1));
}
Solution 3:
Here's a proof of why such a function can't exist, for all numbers, if it doesn't use extra information(except 32bits of int):
We must have f(0) = 0. (Proof: Suppose f(0) = x. Then f(x) = f(f(0)) = -0 = 0. Now, -x = f(f(x)) = f(0) = x, which means that x = 0.)
Further, for any x
and y
, suppose f(x) = y
. We want f(y) = -x
then. And f(f(y)) = -y => f(-x) = -y
. To summarize: if f(x) = y
, then f(-x) = -y
, and f(y) = -x
, and f(-y) = x
.
So, we need to divide all integers except 0 into sets of 4, but we have an odd number of such integers; not only that, if we remove the integer that doesn't have a positive counterpart, we still have 2(mod4) numbers.
If we remove the 2 maximal numbers left (by abs value), we can get the function:
int sign(int n)
{
if(n>0)
return 1;
else
return -1;
}
int f(int n)
{
if(n==0) return 0;
switch(abs(n)%2)
{
case 1:
return sign(n)*(abs(n)+1);
case 0:
return -sign(n)*(abs(n)-1);
}
}
Of course another option, is to not comply for 0, and get the 2 numbers we removed as a bonus. (But that's just a silly if.)