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);
}
$\endgroup$
11
  • 4
    $\begingroup$ There are infinitely many more... $\endgroup$
    – Aryabhata
    May 11, 2011 at 15:51
  • 3
    $\begingroup$ Example: $\sqrt{74 - x^2}$. This can be easily generalized. $\endgroup$
    – Aryabhata
    May 11, 2011 at 15:53
  • 4
    $\begingroup$ Why are you unsatisfied with the simple ones you already have? $\endgroup$ May 11, 2011 at 15:57
  • 5
    $\begingroup$ @Danpe, this isn't a puzzle site. $\endgroup$
    – quanta
    May 11, 2011 at 16:02
  • 5
    $\begingroup$ 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. $\endgroup$ May 11, 2011 at 16:08

1 Answer 1

5
$\begingroup$

return x ^ 2;

Are you sure you aren't missing any constraints?

$\endgroup$
6
  • 2
    $\begingroup$ This doesn't return 5 when x is 7...nor does it return 7 when x is 5 $\endgroup$ May 11, 2011 at 16:12
  • 2
    $\begingroup$ 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. $\endgroup$ May 11, 2011 at 16:20
  • 2
    $\begingroup$ A way to verify this answer: $2 = 5\ \text{XOR}\ 7$. (So basically return x ^ (5 ^ 7);) $\endgroup$
    – Aryabhata
    May 11, 2011 at 16:25
  • $\begingroup$ Woops, my bad. I was naively thinking exponentiation. $\endgroup$ May 11, 2011 at 16:30
  • 2
    $\begingroup$ 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.) $\endgroup$ May 11, 2011 at 16:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.