33
\$\begingroup\$

Challenge

Given an integer \$Q\$ in the range \$-2^{100} ≤ Q ≤ 2^{100}\$, output the number of digits in that number (in base 10).

Rules

Yes, you may take the number as a string and find its length.

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. The number will never have a decimal point.

Zero can either have one or zero digits.

Assume the input will always be a valid integer.

Examples

Input > Output

-45 > 2
12548026 > 8
33107638153846291829 > 20
-20000 > 5
0 > 1 or 0

Winning

Shortest code in bytes wins.

\$\endgroup\$
0

81 Answers 81

2
\$\begingroup\$

FALSE, 14 bytes

^45=[1+^~][]#.

Try it online!

Explanation: If first character is '-' (ascii code 45), then push -1. Else push 0. Add 1 to number on stack until EOF character is reached.

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

Alice, 10 bytes (non-competing)

 /d/
O@IHc

Try it online!

This is a non-competing solution, because at the time this challenge was posted the command c was bugged in the official (and only :D) interpreter. Martin Ender fixed it in the meanwhile, so this now works.

Explanation

The instruction pointer passes through the two mirrors (/) multiple times, so it may be a bit difficult to follow. I'll try to explain it as clearly as I can, using cardinal directions (e.g. N is up, SW is down to the left...). I'll call /1 the leftmost mirror, and /2 the rightmost one.

Command    Direction    Comment
               E        Execution starts from the upper-left corner going right
   /1        E → SE     Passing through the mirror changes direction and switches
                        to ordinal mode (string operations)
   I        SE → NE     Push the input string to the stack, then bounce against
                        the bottom of the code
   /2       NE → S      Back to cardinal mode (numeric operations)
   H           S        Pop n, push abs(n). Execution wraps from bottom to top
   /2        S → SE     Ordinal mode again
   c        SE → NW     Pop s, push each char of s separatedly. Bounce against
                        the bottom right corner
   /2       NW → W      Cardinal mode
   d           W        Push the depth of the stack (which is now equal to 
                        the number of characters in abs(input))
   /1     W → NW → SW   Pass through the mirror, then bounce agains the top
   O        SW → NE     Output the result, then bounce on the bottom left corner
   /1       NE → S      Last mirror, I promise
   @           S        Terminate execution
\$\endgroup\$
1
\$\begingroup\$

REXX 22 Bytes

arg "-" a
say length(a)

Explanation: There is no distinction in Rexx between numbers and strings. The action you perform is what defines the type. The "typing" applies just to that action and can change at any time.

So, here the number (say -20) is treated as a string. The "arg" instruction (short for parse arg) tells Rexx to search for the first "-" and then put everything after it in the variable "a". If "-" is not found then everything goes in "a".

Try it here

REXX functions and instructions

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

GNU Make, 78 bytes

Imperative style:

$(eval T=$1)$(foreach D,$(shell seq 9),$(eval T=$(subst $D,? ,$T)))$(words $T)

Functional style, 113 bytes:

$(eval 2?=$(shell seq 9))$(if $2,$(call $0,$(subst $(word 1,$2),? ,$1),$(wordlist 2,$(words $2),$2)),$(words $1))

Pure Make, 83 bytes:

$(eval T=$1)$(foreach D,0 1 2 3 4 5 6 7 8 9,$(eval T=$(subst $D,? ,$T)))$(words $T)
\$\endgroup\$
1
\$\begingroup\$

C++, 80 76 bytes

#include<string>
int main(int,char**c){printf("%d",strlen(c[1])-(*c[1]<46));}

Prints the length of the argument, minuses 1 if the first character is a minus because bool guarantees conversion to 1 if true or 0 if false

  • 4 bytes thanks to @Squidy for pointing out I can use <46 instead of =='-', and to deference the array instead of []
\$\endgroup\$
3
  • \$\begingroup\$ You could shave off 4 bytes by replacing c[1][0]=='-' with *c[1]<46 since we can assume the input will always be a valid integer. (Unless prefixes other than '-' are allowed...) \$\endgroup\$
    – gsemac
    Commented May 17, 2017 at 23:39
  • \$\begingroup\$ @Squidy oh wow nice find! I racked my brain for ages trying to shorten this and never even came up with that! Thanks for the suggestion, and especially for signing up to PCCG to let me know! \$\endgroup\$
    – Tas
    Commented May 17, 2017 at 23:50
  • \$\begingroup\$ -1 byte by replacing (*c[1]<46) with 45/ *c[1], note the whitespace is necessary, otherwise it would be parsed as the start of a comment \$\endgroup\$
    – c--
    Commented Jul 20, 2022 at 15:05
1
\$\begingroup\$

awk, 22 bytes

$1=length($1<0?-$1:$1)

Try it online!

All test cases

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can save one byte by using sqrt($1^2) to take the absolute value. TIO. \$\endgroup\$
    – Chris
    Commented May 3, 2018 at 2:52
1
\$\begingroup\$

JavaScript, 23 bytes

x=>`${x<0?-x:x}`.length
\$\endgroup\$
1
\$\begingroup\$

Jelly, 5 bytes

A‘l⁵Ċ

Try it online!

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

TI-Basic (TI-84 Plus CE, OS 5.2+), 6 bytes

length(toString(abs(Ans

TI-Basic is a tokenized language; length( and toString( are two bytes each.

Ans is used as implicit input; the last (only) line's value is implicitly returned.

Pretty simple, takes the absolute value to get rid of a minus sign, converts to string, returns the length of the string.

A 6-byte mathematical approach that doesn't work for 0:

1+log(abs(Ans
\$\endgroup\$
2
  • \$\begingroup\$ Which calculators have toString(? \$\endgroup\$
    – kamoroso94
    Commented May 19, 2017 at 5:20
  • \$\begingroup\$ @kamoroso94 TI-84 Plus CE \$\endgroup\$ Commented May 19, 2017 at 16:16
1
\$\begingroup\$

Pari/GP, 13 bytes

n->#digits(n)

Try it online!

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

Excel VBA, 15 Bytes

Anonymous VBE immediate window function that takes input from [A1] and outputs to the VBE immediate window

?[Len(Abs(A1))]
\$\endgroup\$
1
\$\begingroup\$

Bound, 4 bytes

ib_,

Explanation:

i # Get user input
b # Separate each digit of the top element into individual elements
_ # Put the total number of elements onto the stack
, # Remove all but the top element from the stack

Bound then prints the stack at the end of execution, so you can then see the result.

Try it online!

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

GolfScript, 5 bytes

"-"-,

Try it online!

"-"-    # Remove all "-" in the input
    ,   # Get the length of the string
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Extended), 4 bytes

≢∘⍕|

Try it online!

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

BRASCA, 15 bytes

iM:45S=[xx0]x!n

Try it online!

Explanation

i                - Turn the ascii characters 0-9 to numbers
 M:45S=[  0]     - Move the bottom of the stack to the front and check if it's a minus
        xx       - If so: remove the minus
            x    - Remove any excess items on the stack
             !n  - Print the length as a number
\$\endgroup\$
1
\$\begingroup\$

Whispers v2, 39 bytes

> Input
>> |1|
>> "2"
>> #3
>> Output 4

Try it online!

Boring answer. Get input, absolute value, convert to string, return length.

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

Arn -s, 2 bytes

.|

Try it!

Take absolute value, -s takes size. Implicit casting.

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

PowerShell Core, 20 bytes

$args|% *m '-'|% Le*

Try it online!

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

Julia, 7 bytes

ndigits

Try it online!

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

Vyxal, 2 bytes

ȧL

Try it Online!

 L # Length of...
ȧ  # Absolute value of...
   # Implicit input
\$\endgroup\$
1
\$\begingroup\$

Factor + project-euler.common, 13 bytes

number-length

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ this has to be the only language with project euler builtins \$\endgroup\$
    – Razetime
    Commented May 22, 2022 at 13:22
  • \$\begingroup\$ @Razetime Factor's policy of including nearly every contribution with the base language is nice in some ways! \$\endgroup\$
    – chunes
    Commented May 22, 2022 at 13:53
1
\$\begingroup\$

K (ngn/k), 17 bytes

{#$$[x<0;x*-1;x]}

Try it online!

Can't handle big numbers correctly, e.g. 33107638153846291829. Still decent though.

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

Japt, 2 bytes

ìl

Try it

ìl     :Implicit input of integer
ì      :Convert to digit array
 l     :Length
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 11 bytes

{#($x)^"-"}

Try it online!

Easy.

Explanation:

{#($x)^"-"}    Main function. Takes x as input
  ($x)         x as string
      ^"-"     Without string literal "-" (Remove negative symbol)
 #             Length
\$\endgroup\$
1
  • \$\begingroup\$ You can take input as a string and change ($x) to x. \$\endgroup\$
    – naffetS
    Commented Oct 7, 2022 at 18:23
1
\$\begingroup\$

Fig, \$2\log_{256}(96)\approx\$ 1.646 bytes

LA

Try it online!

LA
L  # Length of
 A # The absolute value of the input
\$\endgroup\$
1
\$\begingroup\$

Rust, 51 bytes

fn main(){let f=|n:i128|n.abs().to_string().len();}

Attempt This Online!

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

Fourier, 30 bytes

I~S<0{1}{-2*S~S}S{0}{S^~S}SL^o

Try it on FourIDE!

Fourier doesn't have strings, so it uses logs instead. Bytes are wasted on supporting zero and negative numbers.

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

Befunge, 35 bytes

77*~`!1+:v
~0`#@    ># !#. #- #2_1+

Try it online!

77*~`!            add 49 to the stack and see if that is less than 50 (ascii for
                  '-') 
1+:v              if there is a -, don't count it. Add 1 so we don't
                  have a zero at the top of the stack. Move down.
># !#. #- #2_1+   move right and add one to the total. (Will remove later)
~0`#@             Loop back around and take an input and make sure it is not end of 
                  the string (-1, so we make sure it is > 0)
># !#. #- #2_1+   check to see if there was an input. If there was, add one to the 
                  total. If not, move left. Subtract 2 because we added 1 at two 
                  different locations. Print output and end program.
\$\endgroup\$
0
\$\begingroup\$

Bean, 20 bytes

Hexdump:

00000000: 2650 80d3 d080 a05d 2080 0921 8181 0020  &P.ÓÐ. ] ..!... 
00000010: 8001 dc64                                ..Üd

JavaScript equivalent:

a.match(/\d/g).length;

Explanation:

Implicitly takes input as a decimal string in a and implicitly outputs the length of the match, which is all the decimal digits in the string. Returns 1 for input of 0.

See Demo.

Bean, 22 bytes

Hexdump:

00000000: 2aa0 1f26 4ccc d3a0 8043 53a0 802d 2043  * .&LÌÓ .CS .- C
00000010: 9125 398b 253a                           .%9.%:

JavaScript equivalent:

with(Math)((log10(abs(A))^0)+1);

Explanation:

Implicitly takes input as a decimal integer in A, takes the absolute value, then log-base-10, XORs with 0, then adds 1.

When A is 0, the returned value of log10() is -Infinity, and -Infinity ^ 0 is 0 in JavaScript, so the returned value for 0 is 1.

See Demo.

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

J, 7 bytes

#@":@**

Try it online!

All the solutions I came up with:

0>.@>.10^.>:@(**)
[:#10&#.inv
#@-.&'-'
#@":@**

The first one is math, the second is base conversion, the third takes a string as input, and the shortest takes a number.

The current solution looks like this:

              ┌─ # 
        ┌─ @ ─┴─ ":
  ┌─ @ ─┴─ *       
──┴─ *             

The parent root is a hook between the upper tine and the lower tine. The lower tine (*) is a monad that calculates magnitude. The upper tine is a composition:

         ┌─ # 
   ┌─ @ ─┴─ ":
@ ─┴─ * 

This composes the upper tine (another composition) with *, in this case, a dyad. This is the dyad for multiplication. In the last tine:

   ┌─ # 
@ ─┴─ ":

This calculates format (":) then length (#).

In English form, this is "multiply x times the sign of x, then convert this to a string, then take the length of this string". Basically, abs.toString.length.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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