・ 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.
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))
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.
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)
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.
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!
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]
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
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