6

I am not a mathematician, but I do some computer programming and I am trying to find a solution to a fairly simple problem.

Is there any known formula/equation/function for mathematically determining the number of display digits (aka "places") that will be required when converting one number between two different number bases?

Example: Let us use the number 65535 (base 10) set as the variable N.

  • N base 10 requires 5 decimal digits (N10 = 65535)
  • N base 2 requires 16 binary digits (N02 = 1111111111111111)
  • N base 16 requires 4 hexadecimal digits (N16 = FFFF)

I want to feed N base 10 (N10) and another base into a formula or subroutine of some sort which returns a single number. Using the above example it might look something like this:

  • function(65535,2) returns 16
  • function(65535,16) returns 4

I want to explore some fairly large numbers in several different number bases and I am hoping this can be done as pure math, even if it requires multiple steps, rather than some sort of indexed table that would have to be created for each base.

  • Example: function(1222333444555,99) = ???

PS: Feel free to add any other appropriate tags since I am not sure what else would be right for this question.

O.M.Y.
  • 255

2 Answers2

13

Yes, there is a way: $$f(n, \text{base}) = \log_{\text{base}} (n) + 1\text{.}$$

After you compute the value of $f(n,b)$, you have to use floor function to get the right integer value.

Explanation:

  • for numbers $b^k$, you need $k + 1$ places (digit $1$, followed by $k$ digits $0$)
  • for numbers $b^k -1$, you need $k$ places ($k$ digits $(b - 1)$, e.g., $99999$ for $b = 10$)
  • for numbers $n$ and $m$, such that $n\geq m$, then the number of places for $n$ is greater or equal to the number of places for $m$.

Hence, for all $b^{k - 1} \leq n \leq b^{k}-1$, you need $k$ places. The formula is just a fancy way to say this.

Antoine
  • 3,439
0

Explaining without logarithms, if $b^r\le n < b^{r+1}$, $n $ requires exactly $r$ digits in base $b$.

Bernard
  • 175,478