Find the original challenge here


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


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.


Input > Output

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


Shortest code in bytes wins.

  • \$\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
  • \$\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
  • \$\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\$ – Sriotchilism O'Zaic May 19 '17 at 21:50

34 Answers 34


@yBASIC, 53 bytes.


Input should be stored in variable _%


Rust, 41 bytes

fn f(n:i32)->u8{n!=0&&return f(n/10)+1;0}

Try it online!

  • \$\begingroup\$ Welcome to PPCG. \$\endgroup\$ – Muhammad Salman Apr 22 '18 at 8:25

Pyth, 10 bytes


Test suite

.xhs.l.aQT  # Code
.xhs.l.aQTQ # With implicit variables
            # Print (implicit):
    .l   T  #   the log base 10 of:
      .aQ   #    the absolute value of the input
   s        #   floored
  h         #   plus 1
.x        Q #  unless it throws an error, in which case the input
Python 3 translation:
import math

Gol><>, 12 bytes


Try it online!

Example full program & How it works


1AG       Register row 1 as function G
   IE;    Take input as int, halt if EOF
      GN  Call G and print the result as int

SA            Absolute value
  :z+         Add 1 if zero
     aSL      Take log 10
        S(    Floor
          PB  Increment and return

Many math functions are prefixed with S, which made the code longer.

Adding 0.9 unconditionally (9a,+) and using ceiling (S)) is also possible with same byte count. Zero input yields 0 in this case.


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.