How long is my number: Restricted Version

8

1

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.

Beta Decay

Posted 2017-05-19T13:42:08.043

Reputation: 21 478

I assume no array functions either? – Cyoce – 2017-05-19T16:45:04.387

@Cyoce Yes, no array functions – Beta Decay – 2017-05-19T17:04:41.840

So if a language only accepts input as a string, it's invalid for this challenge, right? – Engineer Toast – 2017-05-19T19:24:15.420

@EngineerToast Yes, very much so – Beta Decay – 2017-05-19T19:33:21.547

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. – Post Rock Garf Hunter – 2017-05-19T21:50:43.623

Answers

9

Mathematica, 13 bytes

IntegerLength

Well...

Martin Ender

Posted 2017-05-19T13:42:08.043

Reputation: 184 808

According to https://codegolf.meta.stackexchange.com/a/3605/14732 this makes this question a duplicate.

– Ismael Miguel – 2017-05-20T12:15:42.127

@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 – 2017-05-20T12:38:53.613

Most answers there can be just copied over to here. – Ismael Miguel – 2017-05-20T13:00:07.423

@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 – 2017-05-20T13:02:48.627

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 – 2017-05-20T13:07:37.573

7

Python 2, 30 bytes

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

Try it online!

Leaky Nun

Posted 2017-05-19T13:42:08.043

Reputation: 45 011

@Notts90 I do, because it is referenced inside. – Leaky Nun – 2017-05-19T17:49:49.007

7

Japt, 5 3 bytes

ì l

Try it online!

Luke

Posted 2017-05-19T13:42:08.043

Reputation: 4 675

1

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 – 2017-05-19T14:28:13.490

Awesome. Well done ninja'ing the question ; ) – Luke – 2017-05-19T14:33:10.267

6

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

Arnauld

Posted 2017-05-19T13:42:08.043

Reputation: 111 334

do we count "f="? lots of the other languages here present the function definition by itself. – Sparr – 2017-05-19T15:06:41.977

5@Sparr This is a recursive function that references itself. So in this special case, yes, we count f=. – Arnauld – 2017-05-19T15:07:49.093

4

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

Luis Mendo

Posted 2017-05-19T13:42:08.043

Reputation: 87 464

DL? – Leaky Nun – 2017-05-19T13:55:38.200

I was trying to do this. But I couldn't find what I needed on the code page :( – Christopher – 2017-05-19T14:01:35.927

"length" of an integer, using the same function that gives the length of a string, really feels like a string function... – Sparr – 2017-05-19T15:04:49.613

1Not 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 – 2017-05-19T15:08:16.997

4

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.

user62131

Posted 2017-05-19T13:42:08.043

Reputation:

3

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())))))

Roman Gräf

Posted 2017-05-19T13:42:08.043

Reputation: 2 915

3

dc, 1 byte

Z

Try it online!


Not using a builtin, 18 bytes:

[d10/d0!=F]dsFxz1-

Try it online!

eush77

Posted 2017-05-19T13:42:08.043

Reputation: 1 280

2

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

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

Try it online!

Returns 1 for 0.

Leaky Nun

Posted 2017-05-19T13:42:08.043

Reputation: 45 011

Why is there no love for SILOS, its golfier than python for this challenge! – Rohan Jhunjhunwala – 2017-05-20T01:32:33.283

2

Lua, 40 bytes

Port from my python answer

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

Try it online!

Felipe Nardi Batista

Posted 2017-05-19T13:42:08.043

Reputation: 2 345

2

Java 8, 61 59 39 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;}

Port of @Khaled.K's C answer.

Try it online.

Kevin Cruijssen

Posted 2017-05-19T13:42:08.043

Reputation: 67 575

1

Python 2, 48 bytes

-3 thanks to ovs -1 thanks to pizzapants

lambda x:math.log10(abs(10*x)+1)//1
import math

Try it online!

Felipe Nardi Batista

Posted 2017-05-19T13:42:08.043

Reputation: 2 345

lambda x:1+log10(abs(x)+.1)//1 for 48 bytes – ovs – 2017-05-19T14:14:28.550

import math and math.log10 saves one byte – pizzapants184 – 2017-05-19T19:47:20.640

1

C#, 49 56 bytes

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

TheLethalCoder

Posted 2017-05-19T13:42:08.043

Reputation: 6 930

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

Leo

Posted 2017-05-19T13:42:08.043

Reputation: 8 482

1

C, 27 bytes

Try Online

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));}

Khaled.K

Posted 2017-05-19T13:42:08.043

Reputation: 1 435

1

R, 40 bytes

function(x)max(ceiling(log10(abs(x))),0)

Try it online!

JayCe

Posted 2017-05-19T13:42:08.043

Reputation: 2 655

0

05AB1E, 6 bytes

Ä>T.nî

Try it online! or Try all tests

Ä      # Absolute value
 >     # Increment
  T.n  # Log base 10
     î # Round up

Riley

Posted 2017-05-19T13:42:08.043

Reputation: 11 345

Ä>T.nî – Leaky Nun – 2017-05-19T13:52:21.643

0

MATL, 5 bytes

|OYAn

Try it online!

Explanation

|     % Implicitly input a number. Absolute value
OYA   % Convert to array of decimal digits
n     % Length. Implicitly display

Luis Mendo

Posted 2017-05-19T13:42:08.043

Reputation: 87 464

0

Jelly, 5 bytes

A‘l⁵Ċ

Try it online!

Christopher

Posted 2017-05-19T13:42:08.043

Reputation: 3 428

0

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.

colsw

Posted 2017-05-19T13:42:08.043

Reputation: 3 195

you should use Floor()+1. Ceil() fails for for powers of 10 – Felipe Nardi Batista – 2017-05-19T14:34:10.513

use $m::Log10(... to save a byte – Felipe Nardi Batista – 2017-05-19T14:41:17.540

0

Octave, 27 bytes

@(x)fix(log10(abs(x+~x)))+1

Try it online!

Luis Mendo

Posted 2017-05-19T13:42:08.043

Reputation: 87 464

0

PHP, 23 Bytes

<?=-~log10(abs($argn));

Try it online!

Jörg Hülsermann

Posted 2017-05-19T13:42:08.043

Reputation: 13 026

0

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

steenbergh

Posted 2017-05-19T13:42:08.043

Reputation: 7 772

0

Bash, 50 bytes

a=$1;until [ $a -eq 0 ];{ let i++ a=a/10;};echo $i

Try it online!

No string/array command, only count digits by integer division.

marcosm

Posted 2017-05-19T13:42:08.043

Reputation: 986

0

Ruby, 33 bytes

->x{1+Math.log10(x.abs+0.1).to_i}

Try it online!

Value Ink

Posted 2017-05-19T13:42:08.043

Reputation: 10 608

0

bc, 6 bytes

length

Built-in function.

eush77

Posted 2017-05-19T13:42:08.043

Reputation: 1 280

0

Actually, 8 bytes

;0=+A╥Lu

Try it online!

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.

Mego

Posted 2017-05-19T13:42:08.043

Reputation: 32 998

0

Pari/GP, 13 bytes

n->#digits(n)

Try it online!

alephalpha

Posted 2017-05-19T13:42:08.043

Reputation: 23 988

0

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

Eric Duminil

Posted 2017-05-19T13:42:08.043

Reputation: 701

0

Perl 5 -p, 26 bytes

$\++;abs($_/=10)<1||redo}{

Try it online!

Xcali

Posted 2017-05-19T13:42:08.043

Reputation: 7 671

0

@yBASIC, 53 bytes.

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

Input should be stored in variable _%

12Me21

Posted 2017-05-19T13:42:08.043

Reputation: 6 110

0

Rust, 41 bytes

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

Try it online!

Arcterus

Posted 2017-05-19T13:42:08.043

Reputation: 11

Welcome to PPCG. – Muhammad Salman – 2018-04-22T08:25:09.677

0

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)

hakr14

Posted 2017-05-19T13:42:08.043

Reputation: 1 295

0

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.

Bubbler

Posted 2017-05-19T13:42:08.043

Reputation: 16 616