Any positive integer can be obtained by starting with 1 and applying a sequence of operations, each of which is either "multiply by 3" or "divide by 2, discarding any remainder".
Examples (writing f for *3 and g for /2):
4 = 1 *3 *3 /2 = 1 ffg
6 = 1 ffggf = 1 fffgg
21 = 1 fffgfgfgggf
Write a program with the following behavior:
Input: any positive integer, via stdin or hard-coded. (If hard-coded, the input numeral will be excluded from the program length.)
Output: a string of f's and g's such that <input> = 1 <string>
(as in the examples). Such a string in reverse order is also acceptable. NB: The output contains only f's and g's, or is empty.
The winner is the entry with the fewest bytes of program-plus-output when 41 is the input.
x mod 3
: ifx=3y
construct y and then applyf
; ifx=3y+1
construct2y+1
and applyf
theng
; ifx=3y+2
then it gets complicated but essentially is recursive. \$\endgroup\$