OFFSET
0,3
COMMENTS
Runtime complexity is O(log log n).
For n < 4 leading zeros are omitted.
For n >=4 all terms are even.
LINKS
Wikipedia, Even-Rodeh coding.
FORMULA
a(n) = a(n-1)+2 for n >= 5 and odd.
EXAMPLE
For n = 2761, a(2761) = 628114 because in binary
n = 101011001001
a(n) = 100 1100 101011001001 0
\-/ \--/ \----------/
4 12 n
n has a 0 appended, then binary 12 prepended since the bit length of n is 12, and then binary 4 prepended since the bit length of 12 is 4, and stop there since 4 is 3 bits long.
Decoding is the reverse: 3 most significant bits are value 4, next 4 bits are value 12, next 12 bits are n, and the bit after is 0 so stop.
PROG
(Python)
def a(n):
if n <= 3: return n
enc, s, v = 0, 0, n
while v > 3:
enc |= v << s
v = v.bit_length()
s += v
return enc << 1
print([a(n) for n in range(63)])
(PARI) a(n) = if (n <= 3, return(n), enc=0; s=0; v=n; while(v >3, enc=bitor(enc, (v << s)); v = #binary(v); s += v; )); return(enc << 1)
CROSSREFS
KEYWORD
nonn,base,easy
AUTHOR
Darío Clavijo, Jan 08 2025
STATUS
approved
