| Bytes | Lang | Time | Link |
|---|---|---|---|
| 019 | JavaScript ES6 | 170519T142130Z | Arnauld |
| 006 | jamforth | 231109T122418Z | allisonl |
| 008 | TIBasic | 231107T211753Z | Youserna |
| 002 | Japt v2.0a0 | 230718T150018Z | Luis fel |
| 001 | Vyxal l | 231106T122758Z | lyxal |
| 007 | Uiua | 231104T185420Z | chunes |
| 025 | Raku | 230718T195021Z | bb94 |
| 005 | Thunno 2 | 230718T141404Z | The Thon |
| 007 | K ngn/k | 201222T195026Z | coltim |
| 033 | Java 8 | 170519T214856Z | Kevin Cr |
| 005 | Vyxal | 210416T070157Z | lyxal |
| 026 | Factor | 210416T023016Z | chunes |
| 026 | Excel | 210416T013752Z | Axuary |
| 080 | Make | 210416T000713Z | cnamejj |
| 046 | PowerShell Core | 210415T213149Z | Julian |
| 007 | Julia | 210413T165926Z | MarcMush |
| 038 | R | 210411T104625Z | Dominic |
| 014 | Pip | 210107T053733Z | Parker K |
| 033 | AWK | 210107T043137Z | Pedro Ma |
| 024 | Perl 5 Minteger p | 180422T044457Z | Xcali |
| 030 | ARM Thumb2 no div instruction | 201225T054039Z | EasyasPi |
| 007 | APL Dyalog Unicode | 201222T192213Z | Kamila S |
| 012 | Gol><> | 180503T071954Z | Bubbler |
| 040 | R | 180503T031054Z | JayCe |
| 010 | Pyth | 180422T215824Z | hakr14 |
| 041 | Rust | 180422T074158Z | Arcterus |
| 053 | @yBASIC | 180422T062043Z | 12Me21 |
| 027 | Ruby | 170520T093927Z | Eric Dum |
| 013 | Pari/GP | 170520T041943Z | alephalp |
| 008 | Actually | 170520T015627Z | user4594 |
| 006 | bc | 170519T210438Z | eush77 |
| 001 | dc | 170519T204614Z | eush77 |
| 027 | C | 170519T194243Z | Khaled.K |
| 048 | Python 2 | 170519T134425Z | Felipe N |
| 033 | Ruby | 170519T185951Z | Value In |
| nan | 170519T181818Z | user6213 | |
| 050 | Bash | 170519T173256Z | marcosm |
| 025 | QBIC | 170519T161608Z | steenber |
| 051 | PowerShell | 170519T142255Z | colsw |
| 005 | Chaincode | 170519T145131Z | Linnea G |
| 023 | PHP | 170519T144408Z | Jör |
| 016 | Alice | 170519T143832Z | Leo |
| 003 | Japt | 170519T140052Z | Luke |
| 027 | Octave | 170519T142730Z | Luis Men |
| 040 | Lua | 170519T142711Z | Felipe N |
| 056 | C# | 170519T135237Z | TheLetha |
| 005 | Jelly | 170519T140047Z | user6318 |
| 002 | Jelly | 170519T135459Z | Luis Men |
| 006 | 05AB1E | 170519T135107Z | Riley |
| 005 | MATL | 170519T135121Z | Luis Men |
| 030 | Python 2 | 170519T134820Z | Leaky Nu |
| 013 | Mathematica | 170519T134514Z | Martin E |
| 041 | S.I.L.O.S | 170519T134423Z | Leaky Nu |
JavaScript (ES6), 19 bytes
f=n=>n&&f(n/10|0)+1
Or 18 bytes with BigInts, as suggested by @Lucenaposition.
jamforth, 6 bytes
UWIDTH
I'm not sure why gforth doesn't have UWIDTH. It's actually really useful.
This solution also works in arbitrary bases.
Japt v2.0a0, 3 2 bytes
Split on each number and get the length. Is this ok? I'm not using any string method implicitly
thanks @The Empty String Photographer
ìl
Vyxal l, 1 byte
ȧ
This is why restricting built-ins is a bad idea. The l flag outputs the length of the top of the stack implicitly, which I don't think is a "function" - it's a feature of the language variant, not a function.
The ȧ simply gets the absolute value because - is counted in the size.
Thunno 2, 5 bytes
A⁺Ælṃ
A⁺Ælṃ # Implicit input
A # Absolute value
⁺ # Increment it
Æl # Log base 10
ṃ # Ceiling
# Implicit output
K (ngn/k), 10 9 7 bytes
-3 bytes from @ngn (see Am I an insignificant array?)
#10\#!:
#!:get the absolute value of the input (literally, take the count of the range of each value; e.g.#!-2->#-2 -1->2)10\"digit-ize" the input#take the count
A solution using $ instead of 10\ save two bytes, but may be invalid given the question's rules.
Java 8, 61 59 39 37 33 bytes
n->(int)Math.log10(n<0?-n:n+.5)+1
-4 bytes thanks to @MarkJeronimus.
Explanation:
n-> // Method with integer as both parameter and return-type
(int) // Convert the following double to an integer (truncating its decimals):
Math.log10(// The log_10 of:
n<0? // If the input is negative:
-n // Use its absolute value
: // Else:
n+.5)// Add 0.5 to the input instead
+1 // And add 1 to the result at the end
Vyxal, 5 bytes
ȧ›∆τ⌈
Thanks to the 05AB1E answer for providing me with an algorithm to port.
Explained
ȧ›∆τ⌈
ȧ› # abs(input) + 1
∆τ # log_10(↑)
⌈ # ceil(↑)
Excel, 26 bytes
=INT(LOG(A1^2+(A1=0))/2)+1
Log of the number squared / 2 is 1 byte shorter than ABS
Make, 80 bytes
$(N):
ifeq ($(N),0)
@echo 0
else
@expr 1 + $(shell make N=$$(($(N)/10)))
endif
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?
PowerShell Core, 46 bytes
param($a)for($s=0;$a){$s+=!!($a=[int]$a/10)}$s
Implementation Details
param($a) # Defines the input parameter
for($s=0;$a){ # Initialise the result to 0 and iterates while the parameter is not 0
$s+=!!($a=[int]$a/10) # Divides the parameter by 10 and increment the result variable
# by one if the result is not 0
}$s # Returns the result
R, 38 bytes
f=function(x)`if`(x^2<100,1,1+f(x/10))
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)...
Pip, 14 bytes
LNABq/LNt//1+1
https://tio.run/##K8gs@P/fx8/RqVDfx69EX99Q2/D/f0MDczNjC1NjCwA
Explanation
LN natural log of... (change of base becasue this is the only log function they had)
ABq the absolute value of the input...
/ divided by...
LNt the natural log of 10... (change of base)
//1 integer divided by 1 (no floor function)
+1 added to 1
This was inspired by the Chaincode solution.
This could probably be optimized.
AWK, 33 bytes
$1=int(log(($1?$1^2:1)^.5)/2.3)+1
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.
ARM Thumb-2 (no div instruction, no libgcc), 30 bytes
Raw machine code:
2800 d00b bfb8 4240 2201 2100 3101 380a
dafc 3201 0008 280a daf7 0010 4770
Uncommented assembly:
.syntax unified
.globl count_digits
.thumb
.thumb_func
count_digits:
cmp r0, #0
beq .Lret
it lt
neglt r0, r0
movs r2, #1
.Lcountloop:
movs r1, #0
.Ldivloop:
adds r1, #1
subs r0, #10
bge .Ldivloop
.Ldivloop_end:
adds r2, #1
movs r0, r1
cmp r0, #10
bge .Lcountloop
.Lcountloop_end:
movs r0, r2
.Lret:
bx lr
Returns 0 if 0.
Explanation
C function signature:
int32_t count_digits(int32_t val);
First, we compare val to zero.
If it is zero, we return zero. If it is less than zero, we negate it.
count_digits:
cmp r0, #0
beq .Lret
it lt
neglt r0, r0
Set up our digit counter for the outer loop.
movs r2, #1
Now, a naïve subtraction based division loop
movs r1, #0
.Ldivloop:
adds r1, #1
subs r0, #10
bge .Ldivloop
Increment the digits counter, then loop to the outer loop if we are still more than 10.
.Ldivloop_end:
adds r2, #1
movs r0, r1
cmp r0, #10
bge .Lcountloop
Move the result into the return register and return.
.Lcountloop_end:
movs r0, r2
.Lret:
bx lr
APL (Dyalog Unicode), 7 bytes
⌈10⍟1+|
Explanation:
⌈10⍟1+|
⌈ ⍝ round up the
10⍟ ⍝ log10 of...
1+ ⍝ incremented
| ⍝ absolute value of input
If format is allowed: 4 bytes - ≢⍕∘|
Gol><>, 12 bytes
SA:z+aSLS(PB
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.
Pyth, 10 bytes
.xhs.l.aQT
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)
@yBASIC, 53 bytes.
_#=@_>.@_
_%=_%/(_#*_#+!.)_=_+!.GOTO(@_)+"_"*!_%@__?_
Input should be stored in variable _%
Ruby, 27 bytes
f=->x{x==0?0:1+f[x.abs/10]}
As a test:
tests = [[-45 , 2],
[1254 , 4],
[107638538 , 9],
[-20000 , 5],
[0 , 0 ],
[-18 , 2]]
tests.each do |i, o|
p f.call(i) == o
end
It outputs:
true
true
true
true
true
true
Actually, 8 bytes
;0=+A╥Lu
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.
C, 27 bytes
f(n){return n?1+f(n/10):0;}
C (gcc), 22 bytes
f(n){n=n?1+f(n/10):0;}
Using math, 29 bytes
f(n){return 1+log10(abs(n));}
Python 2, 48 bytes
-3 thanks to ovs -1 thanks to pizzapants
lambda x:math.log10(abs(10*x)+1)//1
import math
My answer from the other challenge still works:
Brachylog, 1 byte
l
The l builtin is overloaded, but on integers, it takes the number of digits of the integer, ignoring sign.
Bash, 50 bytes
a=$1;until [ $a -eq 0 ];{ let i++ a=a/10;};echo $i
No string/array command, only count digits by integer division.
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
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.
Chaincode, 5 bytes
pqL_+
Note: This is exactly the same code as that from the other challenge
Explanation
pqL_+ print(
+ succ(
_ floor(
L log_10(
pq abs(
input())))))
Alice, 16 bytes
/O
\I@/Hwa:].$Kq
Explanation
/O
\I@/...
This is simply a framework for numerical input→mathematical processing→numerical output.
The rest of the code is the real algorithm:
Hwa:].$Kq
H Compute absolute value
w .$K While the result is not zero do:
a: divide the number by 10
] move the tape head one cell forward
q Get the position of the tape head
C#, 49 56 bytes
namespace System.Math{n=>n==0?1:Floor(Log10(Abs(n))+1);}
Jelly, 3 2 bytes
1 byte saved thanks to Leaky Nun
DL
Explanation
L Length of
D Decimal expansion of input argument. Works for negative values too
05AB1E, 6 bytes
Ä>T.nî
Try it online! or Try all tests
Ä # Absolute value
> # Increment
T.n # Log base 10
î # Round up
MATL, 5 bytes
|OYAn
Explanation
| % Implicitly input a number. Absolute value
OYA % Convert to array of decimal digits
n % Length. Implicitly display