OFFSET
1,2
COMMENTS
For n <= 3 leading zeros are omitted.
In the original Golomb-Rice algorithm, M is an arbitrary power of 2. Here, M is determined as 2^(floor(log_2(floor(log_2(n))+1))+1).
a(2^k) is a divisor of a(2^(k+1)).
LINKS
Paolo Xausa, Table of n, a(n) for n = 1..10000
Wikipedia, Golomb coding
FORMULA
EXAMPLE
For n = 42 a(42) = 111110010_2 = 498.
m = 2^(floor(log_2(floor(log_2(n))+1))+1) = 8 and
q = floor(n / m) = 5 and
r = n mod m = 2 and
u = (2^q-1) left shifted by (2^floor(log_2(m)) + 1) = 111110000_2 = 496 and
u + r = 111110010_2 = 498.
MATHEMATICA
A380294[n_] := (2^Quotient[n, #] - 1)*2^BitLength[#] + Mod[n, #] & [2^Nest[BitLength, n, 2]];
Array[A380294, 100] (* Paolo Xausa, Feb 03 2025 *)
PROG
(Python)
def a(n):
if n <= 3: return n
if n & 1: return a(n-1)+1
m = 1 << (n.bit_length()).bit_length()
q, r = divmod(n, m)
u = ((1 << q) - 1) << m.bit_length()
return u + r
print([a(n) for n in range(1, 57)])
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Darío Clavijo, Jan 19 2025
STATUS
approved
