0
$\begingroup$

I get 5 or 7 and if i get 5 i need to return 7 if i get 7 i need to return 5.

i need to do this in 1 mathematical formula.

I have those:

12 - x

35 / x

There are more solutions ?

Example in code:

public static int Transform(int x)
{
    return (12-x);
    //return (35/x);
}
  • 4
    There are infinitely many more...2011-05-11
  • 0
    Can you show me some ? But smart ones not like `13-x-1`2011-05-11
  • 3
    Example: $\sqrt{74 - x^2}$. This can be easily generalized.2011-05-11
  • 2
    I am voting to close as NARQ.2011-05-11
  • 4
    Why are you unsatisfied with the simple ones you already have?2011-05-11
  • 0
    It's a Puzzle..2011-05-11
  • 5
    @Danpe, this isn't a puzzle site.2011-05-11
  • 2
    This is the same as finding how many functions in the Cartesian plan go through the points $(5,7)$ and $(7,5)$... which is indeed infinite.2011-05-11
  • 5
    If $h$ is an invertible function we can define $f(x) = h^{-1}(h(5) + h(7) - h(x))$. This $f$ has the property that you want. This covers the two methods you have (with $h(x)=x$ and $h(x)=\log(x)$, respectively, and quite a few more.2011-05-11
  • 1
    $f(x) = 12 - x + (x-5)(x-7)h(x)$ or $f(x) = 35/ x + (x-5)(x-7)h(x)$ etc2011-05-11
  • 1
    You can also multiply any formula you've found by (x-6)^2, of course.2011-05-11

1 Answers 1

5

return x ^ 2;

Are you sure you aren't missing any constraints?

  • 2
    This doesn't return 5 when x is 7...nor does it return 7 when x is 52011-05-11
  • 2
    He was using Java or C++ in his original question; ^ is a bitwise XOR. A test confirms that x^2 swaps both 5 and 7 in Visual C++ 10.0.2011-05-11
  • 2
    A way to verify this answer: $2 = 5\ \text{XOR}\ 7$. (So basically `return x ^ (5 ^ 7);`)2011-05-11
  • 0
    Woops, my bad. I was naively thinking exponentiation.2011-05-11
  • 2
    yeah, I read it as exponentiation, too. You might want to make it clear in the answer that it is a bitwise operator. (Mathematicians tend to not think of bitwise operators as "mathematical formulas" as a rule.)2011-05-11
  • 0
    Since this isn't SO, a "where `^` is bitwise XOR" would have went a long way. :)2011-05-11