Getting 5 or 7 and returning the opposite? [closed]

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);
}

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

1 Answer

return x ^ 2;

Are you sure you aren't missing any constraints?

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