# Python – Golomb Encoding for b=2^{n} and b!=2^{n}

The Golomb coding is a form of parameterized coding in which integers to be coded are stored as values relative to a constant b

**Coding:-**

A positive number x is spoken to in two sections:

- The initial segment is an unary portrayal of q+1, where q is the remainder floor((x/b)), and
- The subsequent part is an extraordinary double portrayal of the leftover portion
**r = x-qb**. Note that there are b potential leftovers.

For instance, if b = 3, the potential remnants will be 0, 1 and 2. To spare space, compose the initial couple of remnants utilizing floor(log(b, 2)) bits and the rest utilizing ceil(log(b, 2)) bits. We should do so with the end goal that the decoder knows at the point when floor(log(b, 2)) bits are utilized and when ceil(log(b, 2)) bits are utilized

**Examples:**

Input :N = 37, M = 11Output :0001100

**Code : Python program to implement Golomb Encoding**

`# Python programming for Golomb Encoding` `import` `math` ` ` `# taking input for N and M where ` `# M == 2 ^ n or M != 2 ^ n` `N ` `=` `37` `M ` `=` `11` ` ` `# for finding the value of preceding ` `# number of zeros by dividing N by M` `q ` `=` `N` `/` `/` `M` `# for computing the remainder of N by M` `r ` `=` `N ` `%` `M` ` ` `# appending that many numbers of zeros in` `# starting of the encoded code initially ` `quo ` `=` `'0'` `*` `q` `+` `'1'` ` ` `# for computing the value of b ie floor of ` `# log(M) base 2 which will be used for computing value of k` `b ` `=` `math.floor(math.log2(M))` `k ` `=` `2` `*` `*` `(b ` `+` `1` `)` `-` `M` `# upon comparing the value of remainder with the ` `# value of k if less we we convert remainder r to ` `# binary and add the value from # index 2 because ` `# at index 0, 1 "0b" is present` `if` `r < k:` ` ` `rem ` `=` `bin` `(r)[` `2` `:]` ` ` `l ` `=` `len` `(rem)` ` ` `# upon the calculating value of rem if it is less than` `# computed value of b we add b-1 number of zeros in` `# preceding of the # remainder` ` ` `if` `l<b:` ` ` `rem ` `=` `'0'` `*` `(b` `-` `l)` `+` `rem` `else` `:` `# we convert remainder r to binary and add the ` `# value from index 2 because at index 0, 1 "0b" is present` ` ` `rem ` `=` `bin` `(r ` `+` `k)[` `2` `:]` ` ` `l ` `=` `len` `(rem)` `# upon calculating value of rem if it is less than` `# computed value of b we add b-1 number of zeros in` `# preceding of the # remainder` ` ` `if` `l<b ` `+` `1` `:` ` ` `rem ` `=` `'0'` `*` `(b ` `+` `1` `-` `l)` `+` `rem` `golomb_code ` `=` `quo ` `+` `rem` `print` `(` `"The golomb code encoding for x = {} and b = {} is {}"` `.` ` ` `format` `(N, M, golomb_code))` |

**Output :**

The golomb code encoding for x = 37 and b = 11 is 0001100

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course. And to begin with your Machine Learning Journey, join the **Machine Learning – Basic Level Course**