Convert decimal numbers to n-ary numbers [python]

at first

・ Because beginners are also studying, I would appreciate your guidance. ・ There is an explanation of n-ary numbers at the beginning, so those who know it do not have to read it.

What is a decimal number?

In our world, we basically deal with numbers with the concept of decimal numbers. For example The number 1234 is ... + 10000 * 0 + 1000 * 1 + 100 * 2 + 10 * 3 + 1 * 4 It means omitting [1,10,100,1000,10000, ......], which is a power of 10. (Although the leading 0 is omitted)

If you write "1234" in "Kanji" that represents numbers without omitting this abbreviation in miso

It's tedious to write 1,224, isn't it? (I'm tired of seeing)

I think that writing by omitting the power of 10 is one of the great inventions in the history of mathematics. (Einstein's abbreviation is also called the best invention left by Einstein, and the abbreviation that is too convenient is amazing (small average feeling))

What is an n-ary number?

But as mathematics progressed, mathematicians began to seek romance in generalization (I don't think romance does). You started thinking about generalizing this number notation. So what if we omit the power of n instead of the power of 10? That is the concept of n-ary numbers.

n-ary rule

First of all, the basic rules are the same as in the decimal world. However, there are rules that you may not have been aware of. that is "The coefficient of power of n must be less than n" something like. In the binary world When thinking about a * 4 If a = 3 (2+1)*4 = 8 + 1 * 4 and 8 appears, right? This has the meaning of carry. I know if there is a carry in the middle of the calculation I don't like the carry on the numbers. Of course, there may be a notation that allows carry, but if you allow carry, the notation of the number is not uniquely determined (there are two or more).

For example, if the number 9 is in binary notation Since it is 1 * 8 + 0 * 4 + 0 * 2 + 1 * 1, it can be written as 1001. However, if you allow the carry 08+14+22+11=121 2 is written as 10 in this world 121 = 1 (10) 1 and the writing style is different from 1001 and 9 is expressed in two ways. Of course, we can create a world of mathematics that admits this, Let's use that condition because it is uniquely determined just by setting "the coefficient is less than n". Because that is more convenient. (I haven't really thought about it, but it would be annoying if I got involved with well defined)

Program description

I will make a program to convert it to my favorite base number, but this time I will return each coefficient as a list. The reason is that when you want to convert to a 100-ary number, the coefficient 87 is easy to see. To explain with more concrete examples "8765" is in 100-ary notation It becomes (87) (65). ((87) becomes a one-digit number) However, it is hard to see, so I will write it as [87,65] in the list. The meaning is 87 * 100 + 65 * 1.

Program flow

First, enter the number you want to convert in decimal and ask them to enter the number you want to convert.

N=int(input())#Decimal value you want to convert
K=int(input())#Base number

Oh, I'll make it a function

def change(N,shinsu):

And we will prepare the list to return. It is easy to know the number of digits after conversion, so first find the number of digits

    keta=0
    for i in range(10**9):
        if N<shinsu**i:
             keta+=i
             break

Now you know the number of digits after conversion. In other words, now that we know the size of the list to return, prepare the list.

    ans=[0]*keta

All you have to do is find the coefficient of K ^ x from the beginning.

    for i in range(1,keta+1):
        j=N//(shinsu**(keta-i))
        ans[check]=j
        check+=1
        N-=(j)*(shinsu**(keta-i))

If you return it at the end, you're done

    return ans

To put it together

N=int(input())#Decimal value you want to convert
K=int(input())#Base number

def change(N,shinsu):
    keta=0
    for i in range(10**9):
        if N<shinsu**i:
             keta+=i
             break
    ans=[0]*keta
    check=0
    for i in range(1,keta+1):
        j=N//(shinsu**(keta-i))
        ans[check]=j
        check+=1
        N-=(j)*(shinsu**(keta-i))
    return ans

print(change(N,K))

That's all there is to it!

I tried playing

1023
2
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
9765624
5
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
2853116705
11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Convert n-ary numbers to decimal numbers

Conversely, if a list of n-ary numbers is given, create a function that restores it to decimal numbers.

def henkan(list,shinsu):
    l=len(list)
    ans=0
    for i in range(1,l+1):
        ans+=list[-i]*(shinsu**(i-1))
    return ans

Summary

I personally felt uncomfortable expressing hexadecimal numbers using A, B, C, D, E, and F, so I created it. It is a function I made in the practice of the program and it is quite my favorite function. Thank you for your guidance and encouragement> <

Recommended Posts

Convert decimal numbers to n-ary numbers [python]
python, php, ruby How to convert decimal numbers to n-ary numbers
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
[Python] Convert natural numbers to ordinal numbers
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
[Python] Convert Shift_JIS to UTF-8
Convert IP address to decimal
Convert python 3.x code to python 2.x
Convert CIDR notation netmask to dotted decimal notation in Python
How to convert floating point numbers to binary numbers in Python
Convert markdown to PDF in Python
Convert list to DataFrame with python
Python> list> Convert double list to single list
Python> tuple> Convert double tuple to single tuple
[Python] Change the alphabet to numbers
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to display random numbers (random module)
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
Convert Python date types to RFC822 format
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Sample to convert image to Wavelet with Python
Updated to Python 2.7.9
Convert to HSV
"Backport" to python 2
Convert FBX files to ASCII <-> BINARY in Python
Convert PDF to image (JPEG / PNG) with Python
Convert "number" of excel date to python datetime
Convert PDFs to images in bulk with Python
Python script to convert latitude / longitude to mesh code
[Python] Convert from DICOM to PNG or CSV
Convert svg file to png / ico with Python
Convert Windows epoch values to date with python
How to convert SVG to PDF and PNG [Python]
Convert STL to Voxel mesh using Python VTK
Convert exponential notation float to str in Python
Convert cubic mesh code to WKT in Python
Convert strings to character-by-character list format with python
Convert timezoned date and time to Unixtime in Python2.7
Convert jupyter notebook .ipynb files to python executable .py files
Python practice Convert the input year to the Japanese calendar
How to convert / restore a string with [] in python
How to convert Python # type for Python super beginners: str
Convert a slice object to a list of index numbers
How to install Python
[Python] Convert general-purpose container and class to each other
Convert 202003 to 2020-03 with pandas
Changes from Python 3.0 to Python 3.5