Task

Create and display the sequence of binary digits for a given   non-negative integer.

   The decimal value      5   should produce an output of               101
   The decimal value     50   should produce an output of            110010
   The decimal value   9000   should produce an output of    10001100101000

The results can be achieved using built-in radix functions within the language   (if these are available),   or alternatively a user defined function can be used.

The output produced should consist just of the binary digits of each number followed by a   newline.

There should be no other whitespace, radix or sign markers in the produced output, and leading zeros should not appear in the results.




procedure main()
every i := 5 | 50 | 255 | 1285 | 9000 do
  write(i," = ",binary(i))
end
 
procedure binary(n)                      #: return bitstring for integer n
static CT, cm, cb
initial {
   CT := table()                         # cache table for results
   cm := 2 ^ (cb := 4)                   # (tunable) cache modulus & pad bits 
   }   
 
b := ""                                  # build reversed bit string
while n > 0 do {                         # use cached result ...
   if not (b ||:= \CT[1(i := n % cm, n /:= cm) ]) then {                     
      CT[j := i] := ""                   # ...or start new cache entry
      while j > 0 do 
         CT[i] ||:=  "01"[ 1(1+j % 2, j /:= 2 )]
      b ||:= CT[i] := left(CT[i],cb,"0") # finish cache with padding
      }
   }
return reverse(trim(b,"0"))              # nothing extraneous
end