13
\$\begingroup\$

Find the original challenge here

Challenge

Given an integer, Z in the range -2^31 < Z < 2^31, output the number of digits in that number (in base 10).

Rules

You must not use any string functions (in the case of overloading, you must not pass a string into functions which act as both string and integer functions). You are not allowed to store the number as a string.

All mathematical functions are allowed.

You may take input in any base, but the output must be the length of the number in base 10.

Do not count the minus sign for negative numbers. Number will never be a decimal.

Zero is effectively a leading zero, so it can have zero or one digit.

Examples

Input > Output

-45 > 2
1254 > 4
107638538 > 9
-20000 > 5
0 > 0 or 1
-18 > 2

Winning

Shortest code in bytes wins.

\$\endgroup\$
5
  • \$\begingroup\$ I assume no array functions either? \$\endgroup\$ – Cyoce May 19 '17 at 16:45
  • \$\begingroup\$ @Cyoce Yes, no array functions \$\endgroup\$ – Beta Decay May 19 '17 at 17:04
  • 1
    \$\begingroup\$ So if a language only accepts input as a string, it's invalid for this challenge, right? \$\endgroup\$ – Engineer Toast May 19 '17 at 19:24
  • \$\begingroup\$ @EngineerToast Yes, very much so \$\endgroup\$ – Beta Decay May 19 '17 at 19:33
  • 1
    \$\begingroup\$ I'm removing the restricted source tag because while this is a restriction it is not a real source restriction in that it is not computer tractable. \$\endgroup\$ – Wheat Wizard May 19 '17 at 21:50

46 Answers 46

1
2
1
\$\begingroup\$

AWK, 33 bytes

$1=int(log(($1?$1^2:1)^.5)/2.3)+1

Try it online!

Basically, this will print the integer part of the log base 10 of the input, plus one. 2.3 is a working approximation of natural log of 10. To deal with negative numbers, it takes the square root of the power of two. If input is zero, returns 1 instead, so the pattern is different from 0, which would return false and would not be printed. Too many necessary workarounds.

\$\endgroup\$
1
\$\begingroup\$

R, 38 bytes

f=function(x)`if`(x^2<100,1,1+f(x/10))

Try it online!

Unusually for R, a recursive function seems to be shorter here than the previous answer using built-in functions (although this is mostly gained simply because we can skip abs(x) by using x^2<100 as a condition instead)...

\$\endgroup\$
1
\$\begingroup\$

Make, 80 bytes

$(N):
ifeq ($(N),0)
	@echo 0
else
	@expr 1 + $(shell make N=$$(($(N)/10)))
endif

Try it online!

I'm posting this entry for two reasons...

First, it's a ridiculous way to get the number of digits. :) It's recursively calling make, each time dividing the number by 10, and counting how many time that happens.

Second, it works on my Linux test system from the commandline (which is using GNU Make 4.1) but will not work at all on TIO (which appears to be using GNU Make 4.2.1). So I'm really curious about that and would like to know if it works for other people? It might just be that I don't know how to translate things like:

~/work/code-golf$ make N=888
3

to TIO's input format syntax?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm also not sure about how exactly arguments are managed by TIO, but you can make it work with bash: Try it online! \$\endgroup\$ – Leo 2 days ago
1
\$\begingroup\$

Factor, 26 bytes

[ abs log10 >integer 1 + ]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MATL, 5 bytes

|OYAn

Try it online!

Explanation

|     % Implicitly input a number. Absolute value
OYA   % Convert to array of decimal digits
n     % Length. Implicitly display
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 6 bytes

Ä>T.nî

Try it online! or Try all tests

Ä      # Absolute value
 >     # Increment
  T.n  # Log base 10
     î # Round up
\$\endgroup\$
1
0
\$\begingroup\$

Octave, 27 bytes

@(x)fix(log10(abs(x+~x)))+1

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 23 Bytes

<?=-~log10(abs($argn));

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 52 51 Bytes

$m=[math];$m::Floor($m::Log10($m::Abs($args[0])))+1

Thanks to Felipe for both fixing the Issue with Log10, and providing a 1byte save.

Any System.Math calls are extremely expensive in PowerShell.

Uses the method of getting the Log10 of the Abs Value of the input, and rounding that up.

\$\endgroup\$
2
  • \$\begingroup\$ you should use Floor()+1. Ceil() fails for for powers of 10 \$\endgroup\$ – Felipe Nardi Batista May 19 '17 at 14:34
  • \$\begingroup\$ use $m::Log10(... to save a byte \$\endgroup\$ – Felipe Nardi Batista May 19 '17 at 14:41
0
\$\begingroup\$

QBIC, 25 bytes

≈abs(:)>=1|b=b+1┘a=a/z}?b

This divides the input by 10, and keeps track of how many times we can do this until N < 1.

Explanation:

≈abs(:)>=1| : gets cmd line input, 
            ≈ starts a while loop,
            abs() is literal QBasic code and is for cases with negative n
            | is the terminator to the WHILE-condition
b=b+1       Keep track of the # of divisions        
┘           Syntactic line break
a=a/z       Divide a by 10 (z==10 in QBIC)
}           End WHILE-loop body
?b          PRINT b
\$\endgroup\$
0
0
\$\begingroup\$

Bash, 50 bytes

a=$1;until [ $a -eq 0 ];{ let i++ a=a/10;};echo $i

Try it online!

No string/array command, only count digits by integer division.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 33 bytes

->x{1+Math.log10(x.abs+0.1).to_i}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

bc, 6 bytes

length

Built-in function.

\$\endgroup\$
0
\$\begingroup\$

Actually, 8 bytes

;0=+A╥Lu

Try it online!

Explanation:

;0=+A╥Lu
;0=       is input equal to 0?
   +      add 1 if input is 0, else add 0
    A     absolute value
     ╥L   log base 10, floor
       u  increment

This program effectively calculates floor(log10(x))+1. To deal with log(0) being undefined (actually it returns (-inf+nanj) which is a special way of saying it's undefined), the input is incremented if it is 0 prior to computing the length. Thus, 0 is considered to have a length of 1.

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 13 bytes

n->#digits(n)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Vyxal, 5 bytes

ȧ›∆τ⌈

Try it Online!

Thanks to the 05AB1E answer for providing me with an algorithm to port.

Explained

ȧ›∆τ⌈
ȧ›      # abs(input) + 1
  ∆τ    # log_10(↑)
    ⌈   # ceil(↑)
\$\endgroup\$
1
2

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.