View Single Post
  #4  
Old 01-14-2020, 04:36
chants chants is online now
VIP
 
Join Date: Jul 2016
Posts: 725
Rept. Given: 35
Rept. Rcvd 48 Times in 30 Posts
Thanks Given: 666
Thanks Rcvd at 1,050 Times in 475 Posts
chants Reputation: 48
My formula used bit wise and &, equality comparison ==, implicit cast of true/false to 1/0 and integer multiplication *, and integer addition +. It should work in C or Javascript or what have you. x is the input, the result is the output. Exactly as you specify it.

This works since the last digits are all unique. By bitwise and with 0xf, we get the last digit of the input and compare with each unique last digit. If equal it will result in 1 times the correct output value otherwise 0. Then summed all together maps them properly.

Any one to one mapping can be done without the bitwise and, using only the other 4 operations. Of course it will only work for individual inputs then and will always be zero for not yet seen numbers. Currently it works for individual input last digits that match and is 0 for all other inputs.

Last edited by chants; 01-14-2020 at 04:44.
Reply With Quote
The Following User Says Thank You to chants For This Useful Post:
mcr4ck (01-14-2020)