Actually, this problem come from a programming language challenge.
The objective is: to create a one-to-one function, $f$, that mapped A into B with
A = {0, 1, 2, 3, 4, 5, 6}, and
B = {1, 5, 10, 50, 100, 500, 1000}
and the definition of the function is limited to these operator.
╔═══════════╦════════════════════════════════════╗
║ Operators ║ Function ║
╠═══════════╬════════════════════════════════════╣
║ [] ║ Bit reference ║
║ ** ║ Exponentiation ║
║ *, /, % ║ Multiplication, division, modulus. ║
║ +, - ║ Addition, subtraction. ║
║ <<, >> ║ Left-shift, right-shift. ║
║ & ║ Bitwise AND. ║
║ |, ^ ║ Bitwise OR, Bitwise XOR. ║
╚═══════════╩════════════════════════════════════╝
Operators in descdending order of precedence
Here I want to use as little as possible operator in the function definition.
My solution is:
f(x) = (5 << 85[x]) * 10**((x - 1) / 2)
and f that I get is: {(0, 1), (1, 5), (2, 10), (3, 50), (4, 100), (5, 500), (6, 1000)}
Note:
85[x] means the (x+1)-th digit of the binary representation of 85 ($1010101_2$)
that is 85[0] = 1, 85[1] = 0, 85[2] = 1, etc.
/ is an integer division, so 5/2 = 2.
- My question is, is there a better way to define f, such that the operator that used in the function is as little as possible?
- Is there a mathematic concept that related to this
operator optimization
problem that can I use to model/solve it?