# How long is my number: Restricted Version

## 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.

Input > Output

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

## Winning

Shortest code in bytes wins.

• I assume no array functions either? – Cyoce May 19 '17 at 16:45
• @Cyoce Yes, no array functions – Beta Decay May 19 '17 at 17:04
• So if a language only accepts input as a string, it's invalid for this challenge, right? – Engineer Toast May 19 '17 at 19:24
• @EngineerToast Yes, very much so – Beta Decay May 19 '17 at 19:33
• 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. – Sriotchilism O'Zaic May 19 '17 at 21:50

## Mathematica, 13 bytes

IntegerLength

Well...

• According to codegolf.meta.stackexchange.com/a/3605/14732 this makes this question a duplicate. – Ismael Miguel May 20 '17 at 12:15
• @IsmaelMiguel Well, this is a slightly trickier case, because the challenge is effectively a duplicate in some languages but not at all on others. – Martin Ender May 20 '17 at 12:38
• Most answers there can be just copied over to here. – Ismael Miguel May 20 '17 at 13:00
• @IsmaelMiguel I'd have to go and count, but I believe the majority of answers on the previous challenge used string processing which is not an option here. – Martin Ender May 20 '17 at 13:02
• Meh, I lost the count 3 times. But seems to actually be around 30-45% of the answers. Those can be just copied over. – Ismael Miguel May 20 '17 at 13:07

# Python 2, 30 bytes

f=lambda x:x and-~f(abs(x)/10)

Try it online!

• @Notts90 I do, because it is referenced inside. – Leaky Nun May 19 '17 at 17:49

# Japt, 5 3 bytes

ì l

Try it online!

• I pushed a fix that makes the a unnecessary -- 5 minutes before the challenge was posted :-) Unfortunately, that means it'll only work on the online interpreter. (test it online!) – ETHproductions May 19 '17 at 14:28
• Awesome. Well done ninja'ing the question ; ) – Luke May 19 '17 at 14:33

## JavaScript (ES6), 19 bytes

f=n=>n&&f(n/10|0)+1

console.log(f(-45))       // 2
console.log(f(1254))      // 4
console.log(f(107638538)) // 9
console.log(f(-20000))    // 5
console.log(f(0))         // 0
console.log(f(-18))       // 2

• do we count "f="? lots of the other languages here present the function definition by itself. – Sparr May 19 '17 at 15:06
• @Sparr This is a recursive function that references itself. So in this special case, yes, we count f=. – Arnauld May 19 '17 at 15:07

# Jelly, 3 2 bytes

1 byte saved thanks to Leaky Nun

DL

Try it online!

### Explanation

L    Length of
D     Decimal expansion of input argument. Works for negative values too
• DL? – Leaky Nun May 19 '17 at 13:55
• I was trying to do this. But I couldn't find what I needed on the code page :( – Christopher May 19 '17 at 14:01
• "length" of an integer, using the same function that gives the length of a string, really feels like a string function... – Sparr May 19 '17 at 15:04
• Not lenght of an integer, but of a list of its digits (obtained with D). The challenge says: in the case of overloading, you must not pass a string into functions which act as both string and integer functions This answer follows that rule: I'm not passing a string – Luis Mendo May 19 '17 at 15:08

My answer from the other challenge still works:

# Brachylog, 1 byte

l

Try it online!

The l builtin is overloaded, but on integers, it takes the number of digits of the integer, ignoring sign.

# Chaincode, 5 bytes

pqL_+

## Explanation

pqL_+ print(
+   succ(
_      floor(
L        log_10(
pq           abs(
input())))))

# dc, 1 byte

Z

Try it online!

Not using a builtin, 18 bytes:

[d10/d0!=F]dsFxz1-

Try it online!

# S.I.L.O.S, 41 bytes

i|
lblb
i/10
a+1
if i b
printInt a

Try it online!

Returns 1 for 0.

• Why is there no love for SILOS, its golfier than python for this challenge! – Rohan Jhunjhunwala May 20 '17 at 1:32

# Lua, 40 bytes

print(math.log10(math.abs(10*...)+1)//1)

Try it online!

# Java 8, 615939 37 bytes

n->n==0?1:(int)Math.log10(n<0?-n:n)+1

Port from @TheLethalCoder's C# answer, but without the Math.floor because using an (int)-cast automatically floors/truncates decimals in Java.

Try it online.

Recursive Java 7 answer (61 38 bytes):

int c(int n){return n!=0?1+c(n/10):0;}

Try it online.

# C#, 49 56 bytes

namespace System.Math{n=>n==0?1:Floor(Log10(Abs(n))+1);}

# Alice, 16 bytes

/O
\I@/Hwa:].$Kq Try it online! ### 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

Try it online!

# 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. • you should use Floor()+1. Ceil() fails for for powers of 10 – Felipe Nardi Batista May 19 '17 at 14:34 • use$m::Log10(... to save a byte – Felipe Nardi Batista May 19 '17 at 14:41

## 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

Try it online!