Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Binary-digits/Icon/binary-digits.icon
Normal file
23
Task/Binary-digits/Icon/binary-digits.icon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue