0

I was wondering if it was possible to, say, have function $f$ that would return the number of digits in any given positive integer. I tried using some sort of a summation, but that failed quite miserably.

Using piecewise equations, it might be constructed something like this: $$f(x)=\begin{cases}1 & \text{ if } x=1 \\2 & \text{ if } x=52 \\3 & \text{ if } x=230 \\4 & \text{ if } x=5023\\\vdots&\text{ }\vdots\end{cases}$$ This is, however, not what I want. Instead, I would like, if possible, some mathematical way of doing this.

2 Answers2

3

$$f(x) = 1+\lfloor \log_{10}(x)\rfloor$$ More generally, $$f(x) = 1+\lfloor \log_{b}(x)\rfloor$$ returns the number of digits of the number in base $b$.

Adhvaitha
  • 5,441
2

Hint: The integer part of the logarithm in base 10 is the function you look for.

rewritten
  • 3,092
  • Not quite. It's one more than that. – MPW Dec 02 '14 at 22:48
  • Of course, I was pointing out which function the OP would want to use to find the solution. Notice the difference between "the solution is" and "you need to look for". – rewritten Dec 02 '14 at 22:51
  • I accept that. But it isn't at all clear in your answer. OP specifically asks for a function that gives the answer he seeks, and you stated that your function is the one he is looking for. If you are suggesting a gentle hint instead of an explicit answer (which I encourage, incidentally), it's usually an excellent idea to preface your answer with the disclaimer "Hint:" to make that clear. I can't withdraw my downvote unless you edit your answer because the site won't let me. – MPW Dec 02 '14 at 23:00
  • @MPW edited. :) – rewritten Dec 02 '14 at 23:32