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)) ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Encoding and Decoding Base64 Strings in Python
- Encoding and Decoding Custom Objects in Python-JSON
- Run Length Encoding in Python
- ML | Label Encoding of datasets in Python
- Python | C Strings of Doubtful Encoding | Set-2
- Python | C Strings of Doubtful Encoding | Set-1
- ML | One Hot Encoding of datasets in Python
- Python | Character Encoding
- Python | Encoding Decoding using Matrix
- response.encoding - Python requests
- Elias Gamma Encoding in Python
- Python – Categorical Encoding using Sunbird
- Mean Encoding - Machine Learning
- Feature Encoding Techniques - Machine Learning
- One Hot Encoding using Tensorflow
- Important differences between Python 2.x and Python 3.x with examples
- Creating and updating PowerPoint Presentations in Python using python - pptx
- Competitive Coding Setup for C++ and Python in VS Code using Python Script
- Loops and Control Statements (continue, break and pass) in Python
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.