$\mathfrak{Mini}$ $\mathbb{Wiki}$

1. June 2020 mozgan   

Basis Representation

For theoretical background see this page.

Let $n$ be a positive number in decimal system and $k$ be the basis in which $n$ will be represented. The following code give us the representation of $n$ in basis $k$.

#!/usr/bin/python
 
def representation(n, k):
    (q, r) = (n // k, n % k)
 
    # print(str(n) + " = " + str(k) + " * " + str(q) + " + " + str(r))
    if q == 0:
        return [r]
    else:
        return representation(q, k) + [r]

Example: Let $n = 383$ and $k=4$.

n = 383
k = 4
 
print(''.join(map(str, representation(n, k))))

The solution is $11333$.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies