0
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ Try $d(x)=1 + \lfloor \log_{10} x \rfloor$. $\endgroup$ – mjqxxxx Dec 2 '14 at 22:28
  • $\begingroup$ d(2) = 1.3 so I have 1.3 digits? $\endgroup$ – MathApprentice Dec 2 '14 at 22:31
  • 2
    $\begingroup$ @MathApprentice You didn't take the floor first: $\log_{10}(2)\approx .3$. So, you take the floor of that and get $0$. So, $d(2)=1+0=1$. $\endgroup$ – Joe Johnson 126 Dec 2 '14 at 22:43
  • 1
    $\begingroup$ You may be interested in my answer to this related question: math.stackexchange.com/questions/795412/… $\endgroup$ – MPW Dec 2 '14 at 22:45
  • $\begingroup$ @MPW Thanks, very interesting. $\endgroup$ – Conor O'Brien Dec 2 '14 at 22:48
3
$\begingroup$

$$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$.

$\endgroup$
  • 1
    $\begingroup$ so, $1 + log_{10} (2) = 1.3$ so I have 1.3 digits? $\endgroup$ – MathApprentice Dec 2 '14 at 22:30
  • 1
    $\begingroup$ $f(x) = 1+\lfloor \log_{b}(x)\rfloor$, $f(n)=-1$, if $0<n<.1$ $\endgroup$ – Conor O'Brien Dec 2 '14 at 22:32
  • 3
    $\begingroup$ @MathApprentice Don't you see the floor function? $\endgroup$ – Adhvaitha Dec 2 '14 at 22:43
  • $\begingroup$ Oh that's what the half-brackets mean. Okay. $\endgroup$ – MathApprentice Dec 2 '14 at 22:47
2
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Not quite. It's one more than that. $\endgroup$ – MPW Dec 2 '14 at 22:48
  • $\begingroup$ 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". $\endgroup$ – rewritten Dec 2 '14 at 22:51
  • $\begingroup$ 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. $\endgroup$ – MPW Dec 2 '14 at 23:00
  • $\begingroup$ @MPW edited. :) $\endgroup$ – rewritten Dec 2 '14 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.