Calculation of the length required to represent an integer in an arbitrary base

I have the length of an integer representation in an arbitrary base. Let's say that the length is 15, and the base is 36. I would like to figure out how long the representation of the integer will be in another arbitrary base. that is, conversion to base 2 may result in a length of 68.

I know this in accordance with the following, but I can’t understand what I need to sink and sink, and I get some results that go away:

length * log(fromBase) / log(toBase) 
+4
source share
3 answers

Following the syntax of type Mathematica, let

 Log[b,n] 

represent the logarithm for base b of n. Let Log[n] represent the natural logarithm of n .

Then the ratio

 Log[b1,n]/Log[b2,n] 

constant and equal

 Log[b2]/Log[b1] 

This ratio is a factor for calculating the number of digits in the base b1 from the number of digits in the base b2 (or vice versa, if you see so). For example, in question, a 15-digit base number-36 is needed

 15*Log[36]/Log[2] == 77.5489 

base-2 digits. This, of course, is exactly what you have in your question. You only need to round the final answer to the next integer.

I'm not sure, of course, why you seem to get some results that go away.

+6
source

Unfortunately, there is no exact solution without high precision calculations. For example, (I will use MATLAB for my work, including the precision tools that I wrote myself), what is 2 ^ 200? In base 10 we get:

 vpij(2)^200 ans = 1606938044258990275541962092341162602522202993782792835301376 

This number is presented in binary format using 201 base 2 digits. However, for 2 ^ 200-1, only 200 base 2 digits are required.

 vpij(2)^200 - 1 ans = 1606938044258990275541962092341162602522202993782792835301375 

Now we can calculate the log of these numbers as double, taking only higher-order digits. We need to add 1 to base 2 of the logarithm of a number to find out how many digits of base 2 are needed to represent it.

 format long g 1 + log2(vpij(2)^200) ans = 201 1 + log2(vpij(2)^200 - 1) ans = 201 

Here log2 did just that, taking the upper decimal digits to compute this log. See that he can’t say that the second number really requires that less bit is stored in binary form.

 vpij2bin(vpij(2)^200) ans = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 vpij2bin(vpij(2)^200 - 1) ans = 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 

We can see what happens by taking a high precision journal of these numbers. Thus, with an accuracy of 100 decimal places,

 log2(hpf(2,100)^200) ans = 200 log2(hpf(2,100)^200 - 1) ans = 199.9999999999999999999999999999999999999999999999999999999999991022086719253476184905817230522465495 

The difference between the two numbers is very small.

 log10(hpf(2,100)^200) - log10(hpf(2,100)^200 - 1) ans = 2.702621195974725251000559400026211938865e-61 

Thus, any calculation using logs should be interrupted here if the high precision log itself is not accepted. In the best case, you may be in the right number, but nothing more. Therefore, if your goal is to allocate enough space for a number, then always highlight another digit than is necessary. That should be enough until you start working with REALLY huge powers.

(VPIJ is the new integer form of integer variable precision in MATLAB, which will directly replace my old VPI tool. HPF is already available in file sharing.)

+3
source

You can get the exact answer without using logarithms. Raise the radios of an arbitrary base until the number goes inside.

Python example:

 def count_digits(number, base): radix = 1 while number >= base ** radix: radix += 1 return radix 
+3
source

All Articles