30
\$\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

72 Answers 72

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

Bash, 15 bytes

wc -L<<<${1//-}

Try it online!

Deletes - from input as array, and otputs length of longest line (wc -c returns one char more than length)

\$\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
    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
    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--
    Jul 20 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
    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
    May 19, 2017 at 5:20
  • \$\begingroup\$ @kamoroso94 TI-84 Plus CE \$\endgroup\$ 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
    May 22 at 13:22
  • \$\begingroup\$ @Razetime Factor's policy of including nearly every contribution with the base language is nice in some ways! \$\endgroup\$
    – chunes
    May 22 at 13:53
1
\$\begingroup\$

K (ngn/k), 17 bytes

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

Try it online!

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

\$\endgroup\$
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\$
0
\$\begingroup\$

C#, 33 bytes

i=>Math.Abs(i).ToString().Length;
\$\endgroup\$
1
  • \$\begingroup\$ You need to fully qualify Math and you can save bytes by using +"" instead of ToString(). \$\endgroup\$ May 17, 2017 at 14:51
0
\$\begingroup\$

MATL, 4 bytes

47>s

Inspired by Martin's Retina answer: count how many characters have code point exceeding 47 (this excludes the minus sign).

Try it online!

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

Braingolf, 2 bytes

dl

Explaination:

dl
    Implicit input from command-line args to stack
d   Pop last item from stack, split it into digits, and push each digit to the stack
 l  Push length of stack to the end of the stack
    Implicit output of last item on stack
\$\endgroup\$
0
\$\begingroup\$

Brain-Flak, 48 + 3 ( -a) = 51 bytes

({}[((((()()()()())){}{})){}{}]){(<><>)}{}([]<>)

Try it online!

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

C#, 28 bytes

s=>s.Replace("-","").Length;
\$\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.