3

I expect I can get length of decimal digit from function with given number input.

For example:

  • $f(9)=1$
  • $f(95)=2$
  • $f(529)=3$

And so on...

What is the general form of $f(x)$?

2 Answers2

3

Are you looking for $$f(x)=\lfloor\log_{10}x\rfloor+1$$

Parcly Taxel
  • 103,344
Bobby Ocean
  • 3,205
  • Yes, f(x)=floor(log(x)) + 1 is already simplified version from your answer. – Muhammad Ikhwan Perwira Sep 03 '22 at 05:46
  • Yes of course, assuming $log$ means base 10. I prefer to emphasize the base. – Bobby Ocean Sep 03 '22 at 05:49
  • @ParclyTaxel I would prefer you not edit a matter of preference. It is standard to write ratios of the natural logarithm rather than writing a logarithm with a base. I have whole complex and real variables books that do this throughout. – Bobby Ocean Sep 03 '22 at 05:59
  • If you try implementing this in any programming language, you will run into problems due to rounding errors - don’t be surprised if n=10^9 gives a result of 8. The result is undefined instead of 1 for x = 0. – gnasher729 Sep 03 '22 at 06:46
  • Are you sure about that? Any number that fits into memory will process through a logarithm and have a perfectly accurate whole part within that same buffer size. – Bobby Ocean Sep 03 '22 at 10:11
2

If you have a positive integer $N$ represented in basis $B$, then the number of digits needed is exactly$\def\len{\operatorname{len}}$

$$\len_B(N)=1+\lfloor\log_B N\rfloor$$

where $\lfloor\,\cdot\,\rfloor$ denotes the floor function.

emacs drives me nuts
  • 10,390
  • 2
  • 12
  • 31