I want to convert any positive integer to some base from 2
to 9
.
The output for base 2
should be:
0 -> enlist 0
1 -> enlist 1
31 -> 1 1 1 1 1
62 -> 1 1 1 1 1 0
63 -> 1 1 1 1 1 1
64 -> 1 0 0 0 0 0 0
I developed the next function for this
convertToBase: reverse {[x;base;result]
result,: `int$x mod base;
x-: last result;
x%: base;
$[x=0;result;.z.s[x;base;result]]
};
//invocation
convertToBase[62;2;()]
The question is, if there is any build-in Q
function to do it effectively? If not, how above solution can be optimized?