Search code examples
mathradixlogarithm

Calculating the length needed to represent an integer in an arbitrary base


I have the length of a representation of an integer in an arbitrary base. Say the length is 15, and the base is 36. I'd then like to work out how long a representation of said integer would be in another arbitrary base. i.e, converting to base 2 might result in a length of 68.

I know it's along the lines of the below, but I can't quite get my head around what I need to floor and ceil, and I'm getting some results that are way off:

length * log(fromBase) / log(toBase)

Solution

  • Following a Mathematica-like syntax, let

    Log[b,n]
    

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

    Then the ratio

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

    is constant, and equal to

    Log[b2]/Log[b1]
    

    This ratio is a multiplier for calculating the number of digits in base b1 from the number of digits in base b2 (or vice-versa if you see things that way). For the example in the question, a 15-digit base-36 number will need

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

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

    I'm not sure, of course, why you seem to be getting some results that are way off.