I needed to convert a bunch of decimals to base 4, and wondered how short it could get. This is my best so far:

perl -le'$!=shift;$_.=$!%4,$!=$!/4 while$!;print"".reverse' 62
.. for 54 characters excluding the "perl", and the argument.

Hugo

Replies are listed 'Best First'.
Re: base 4 golf
by thospel (Hermit) on Feb 22, 2003 at 18:46 UTC
    perl -le's//$%%4/ewhile$%=$%/4+pop;print' 62
Re: base 4 golf
by Aristotle (Chancellor) on Feb 22, 2003 at 21:04 UTC
    Not as short, but quite elegant I thought.
    # 1 2 3 4 # 12345678901234567890123456789012345678901234567 perl -e'$_=sprintf"%b",pop;s/(.)(.)/print$1*2+$2/eg' 62

    Makeshifts last the longest.

      Can be shortened to:
      perl -le'$_=sprintf"%b",pop;s/../$&%8/eg;print'
      Fails in case the binary expension has odd length. can be fixed like this for example:
      perl -le'$_=sprintf"%b",pop;s/..(?=(..)*$)/$&%8/eg;print' 31

      update (broquaint): added formatting

Re: base 4 golf
by theorbtwo (Prior) on Feb 23, 2003 at 14:31 UTC

    BTW, you might want to see DNA that is a JAPH for the reverse operation. It isn't very golfed, nor very obscure, I'm afraid.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).