8
\$\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\$
  • \$\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

0
\$\begingroup\$

@yBASIC, 53 bytes.

_#=@_>.@_
_%=_%/(_#*_#+!.)_=_+!.GOTO(@_)+"_"*!_%@__?_

Input should be stored in variable _%

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

Rust, 41 bytes

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

Try it online!

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

Pyth, 10 bytes

.xhs.l.aQT

Test suite

Explanation:
.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
Q=eval(input())
try:
    print(int(math.log(abs(Q),10))+1)
except:
    print(Q)
\$\endgroup\$
0
\$\begingroup\$

Gol><>, 12 bytes

SA:z+aSLS(PB

Try it online!

Example full program & How it works

1AGIE;GN
SA:z+aSLS(PB

1AGIE;GN
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:z+aSLS(PB
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.

\$\endgroup\$

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.