24 lines
824 B
Plaintext
24 lines
824 B
Plaintext
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
|