4 ms·
f(n)=in Where i=sqrt(-1), either would do. Without proper constraints, the problem is trivial. The fact that the variable is called 'n' means nothing. OTOH,
by pfortuny 13y ago
f(n)=in
Where i=sqrt(-1), either would do.
Without proper constraints, the problem is trivial. The fact that the variable is called 'n' means nothing.
OTOH, if f is integer-valued and of an integer variable,
f(0)=0
f(2n)=-(2n-1)
f(2n-1)=2n
f(-(2n-1))=-2n
f(-2n)=2n-1
Unless I am pretty confused (which may be).
The difficult one is f(f(n))=n^2.
- comex 13y agon is a 32-bit signed integer, as stated in the link.
- pfortuny 13y agoYes, my formula is easily transformed for 32-bit integers with sign. Not that difficult. I leave that as an exercise to the reader. :-)
- comex 13y agoIt is not "not that difficult": as one of the answers explained, it is impossible to create such a function that works for all values - not just -2147483648, which obviously cannot comply, but in your case, also 2147483647.
- rcfox 13y ago1) They disallowed complex numbers. 2) You can't just do an operation to the right side and then do the inverse inside of the function on the left side. It has to be done outside of the function. 3) You didn't accomplish anything with your butchered math. We wanted f(f(n)) = -n
- pfortuny 13y agoI guess you do not understand my formula... Given z a non-zero integer, either z=2n or z=2n-1 or z=-2n or z=-(2n-1), for some natural n>0... f(n) is defined according to each case. Uhu? And AFAIK f(f(n))=-n. This should not be too hard to express in 32 bit arithmetic. With bitwise shifts etc.
- richardkiss 13y agof(f(n)) = n^2 is actually easy when you consider it restricted to 32-bit signed integers. Since it's impossible to make it work for any abs(n)>65536 (it's too big), you can reserve all those values as intermediate stopover points for the ones it WILL work for. f(n) = abs(n) + 65536 if n < 65536 = unsigned32(n & 0xffff) ** 2 if n >= 65536 Cheeky, eh?
- pfortuny 13y agoAh, yes. But I was thinking of all the integers. I know, I should have read the OP but I was too lazy. Really cheeky, though :)