# How long is the number in this base?

Given a positive integer $$\n\$$ and another positive integer $$\b\$$ ($$\1 < b < 36\$$), return the number of digits/length of $$\n\$$ in base $$\b\$$

1597 16 -> 3
1709 9 -> 4
190 29 -> 2
873 24 -> 3
1061 27 -> 3
289 26 -> 2
1575 34 -> 3
1135 15 -> 3
1161 22 -> 3
585 23 -> 3
1412 23 -> 3
1268 14 -> 3
714 12 -> 3
700 29 -> 2
1007 35 -> 2
292 17 -> 3
1990 16 -> 3
439 3 -> 6
1212 17 -> 3
683 31 -> 2
535 25 -> 2
1978 32 -> 3
153 8 -> 3
1314 33 -> 3
433 2 -> 9
655 35 -> 2
865 19 -> 3
1947 25 -> 3
1873 32 -> 3
1441 12 -> 3
228 30 -> 2
947 2 -> 10
1026 11 -> 3
1172 24 -> 3
1390 32 -> 3
1495 21 -> 3
1339 10 -> 4
1357 9 -> 4
1320 27 -> 3
602 5 -> 4
1462 16 -> 3
1658 9 -> 4
519 11 -> 3
159 3 -> 5
1152 11 -> 3
1169 33 -> 3
1298 7 -> 4
1686 32 -> 3
1227 25 -> 3
770 15 -> 3
1478 20 -> 3
401 22 -> 2
1097 7 -> 4
1017 9 -> 4
784 23 -> 3
1176 15 -> 3
1521 7 -> 4
1623 23 -> 3
1552 13 -> 3
819 15 -> 3
272 32 -> 2
1546 12 -> 3
1718 4 -> 6
1686 8 -> 4
1128 2 -> 11
1617 34 -> 3
1199 34 -> 3
626 23 -> 3
991 9 -> 4
742 22 -> 3
1227 11 -> 3
1897 12 -> 4
348 35 -> 2
1107 11 -> 3
31 26 -> 2
1153 26 -> 3
432 4 -> 5
758 9 -> 4
277 21 -> 2
472 29 -> 2
1935 21 -> 3
457 27 -> 2
1807 26 -> 3
1924 26 -> 3
23 27 -> 1
558 30 -> 2
203 15 -> 2
1633 8 -> 4
769 21 -> 3
1261 32 -> 3
577 7 -> 4
1440 22 -> 3
1215 35 -> 2
1859 23 -> 3
1702 35 -> 3
1580 12 -> 3
782 15 -> 3
701 32 -> 2
177 24 -> 2
1509 28 -> 3


Shortest code in bytes wins.

• consider adding test case with 0 like 0 2 -> 1? a lot of them seem to fail with it Commented Nov 4, 2021 at 13:19
• @Hydrazer But 0 is not a positive integer?
– ovs
Commented Nov 4, 2021 at 14:29
• oh i guess you're right Commented Nov 4, 2021 at 21:32

# JavaScript (V8), 21 bytes

b=>g=n=>n&&1+g(n/b|0)


Try it online!

# Python 2, 28 bytes

f=lambda x,b:x and-~f(x/b,b)


Try it online!

thanks @Kevin Cruijssen for -1 byte by using python 2 instead of python 3

• You could switch to Python 2 so the // can be /. Commented Nov 4, 2021 at 12:38
• i think you need to include the function name if you are doing recursion Commented Nov 4, 2021 at 13:12

# Zsh, 20 bytes

<<<${#v=$[[##$2]$1]}


Full program taking $$\n\$$ and $$\b\$$ as arguments.

### Commented

<<<                  # print
${# } # length in characters of v= # set dummy variable v to$[[##$2]$1]  # n ($1) in base b ($2)


Here's an alternative offered by @pxeger. The byte count is the same but the dummy variable is elegantly avoided using parameter expansion.

<<<${#:-$[[##$2]$1]}


Attempt This Online!

• You can avoid defining v with the ${:-} construct: , which can substitute a literal string without changing any variables, exactly to workaround issues like this. Attempt This Online!. (It's the same byte count, though) Commented Nov 5, 2021 at 8:01 # APL (Dyalog Extended), 2 bytes (SBCS) Anonymous tacit infix function taking $$\b\$$ as left argument and $$\n\$$ as right argument. ≢⊤  Try it online! ≢ tally the digits of ⊤ the anti-base (i.e. the representation in the given base) Note that this is wasteful in that it actually does the base conversion. ## APL (Dyalog Unicode), 4 bytes Anonymous tacit infix function taking $$\b\$$ as left argument and $$\n\$$ as right argument. ⌊1+⍟  Try it online! ⌊ floor 1+ the incremented ⍟ log Implements $$\⌊1+\log_bn⌋\$$. • Could you use ceiling instead of increment and floor? Commented Nov 4, 2021 at 11:40 • @Graham No, because log₁₀1000 is 3 but 1000 takes 4 digits. – Adám Commented Nov 4, 2021 at 11:41 • How is ≢⊤ calculated as "2 bytes"? I.e. what encoding is used? Commented Nov 6, 2021 at 10:43 • @Feuermurmel I've updated the post heading to explain. – Adám Commented Nov 6, 2021 at 18:46 # Pyth, 4 3 bytes ljF  Try it online!  # implicitly output l # len of jF # convert first element in input to base second element of input  -1 byte thanks to Mr. Xcoder • ljF saves a byte if you take input in the form [a,b] (or even a,b) Commented Nov 7, 2021 at 7:59 # Charcoal, 5 bytes ＩＬ↨ＮＮ  Try it online! Link is to verbose version of code. Works for any b>1. Unfortunately Base doesn't accept implicit arguments, so I have to explicitly input the n and b. Explanation:  Ｎ Input n as an integer Ｎ Input b as an integer ↨ Convert n to base b as an array Ｌ Take the length Ｉ Cast to string Implicitly print  • Note: This solution returns 0 for n=0; switching to BaseString means that it returns 1 for n=0 but then it only works up to base 62 (which is still within the range required by the question). – Neil Commented Nov 4, 2021 at 17:07 # Retina 0.8.2, 35 bytes \d+$*
+\b(1+) (\1)+1*
$1$#2$*  Try it online! Link includes test cases. Takes input in the order b, n. Explanation: \d+$*


Convert b and n to unary.

+\b(1+) (\1)+1*
$1$#2$*  Repeatedly integer divide n by b until n is less than b. With each division, prefix a space to b.   Count the total number of spaces, including the original space. This gives the desired answer. # Ruby, 21 bytes ->n,b{/$/=~n.to_s(b)}


Try it online!

# 05AB1E, 2 bytes

вg


First input is the base $$\b\$$, second input is $$\n\$$.

Using logarithm like most other answers is 4 bytes:

.n>ï


Explanation:

      # First (implicit) input-integer = b
# Second (implicit) input-integer = n

в     # Convert n to a base-b list
g    # Pop and get the length of that list
# (after which the result is output implicitly)

.n    # Log_b(n)
>   # +1
ï  # Truncate decimals
# (after which the result is output implicitly)


# Wolfram Language (Mathematica), 13 bytes

IntegerLength


Try it online!

Of course there's a built-in. Input [n, b].

### Wolfram Language (Mathematica), 15 bytes

1+⌊Log@##⌋&


Try it online!

Not the built-in. Input [b, n].

• How is #2 15 bytes? Commented Nov 5, 2021 at 3:33
• @emanresuA ⌊⌋ are each 3 bytes.
– att
Commented Nov 5, 2021 at 3:53

# Japt v1.4.5, 4 3 bytes

slV


Try it here

# Factor, 16 bytes

[ >base length ]


Try it online!

It's a quotation (anonymous function) that takes a number and a base (in that order) and returns the length of the number in the given base.

• >base Take a number and a base and return the number in the given base as a string.
• length The length of the string.

# Vyxal, 2 bytes

τL


Try it Online!

Simplest method

τ does the base conversion, and L gives the length.

• ṙ rounds, so #2 is wrong Commented Nov 5, 2021 at 3:35

# Jelly, 2 bytes

bL


Try it online!

Finally, a Jelly answer using ASCII only.

bL - Main link. Takes a number on left, base on right
b  - Obtain left base right
L - Get the length of the string representation of the result


# Pascal, 20 characters

Plain translation of the formula: $$\left\lfloor \frac{\log_c n}{\log_c b} + 1\right\rfloor$$

trunc(ln(n)/ln(b)+1)


# JavaScript (V8), 25 bytes

f=(n,b)=>n<b?1:1+f(n/b,b)


Try it online!

# JavaScript (V8), 26 bytes, thanks to Adám

n=>b=>n.toString(b).length


Try it online!

# JavaScript (V8), 32 bytes

f=(n,b,x=1)=>b**x<n?f(n,b,x+1):x


Try it online!

• Any reason to avoid n=>b=>n.toString(b).length?
Commented Nov 4, 2021 at 10:07
• Recursive division approach would be shorter still.
– Neil
Commented Nov 4, 2021 at 10:08
• @Adám well I wasn't aware! Commented Nov 4, 2021 at 10:08
• 22 bytes by taking the arguments in reverse order: b=>g=n=>n<b?1:1+g(n/b)
– Neil
Commented Nov 4, 2021 at 11:05
• @Neil, 21 bytes Commented Nov 4, 2021 at 23:40

# R, 27 bytes

Or R>=4.1, 20 bytes by replacing the word function with \.

function(n,b)log(n,b)%/%1+1


Try it online!

# Raku, 21 bytes

This solution works with n = 0

{$^a.base($^b).chars}

$^a and$^b are implicit when you call a function with 2 arguments
This is similar to if you call a function with 1 argument
That function will have implicit topic variable $_  Try it online! # TI-Basic, 12 bytes Prompt N,B 1+int(log(N,B  Output is stored in Ans and is displayed. If not using OS 2.53 MP or higher, log(N,B should be replaced with log(N)/log(B to add 2 bytes. # MATL, 3 bytes YAn  Try it online! YAn YA : dec2base n : numel/size  # Desmos, 23 21 bytes f(n,b)=floor(log_bbn)  Try It On Desmos! Try It On Desmos! - Prettified # C (gcc), 26 25 bytes f(n,b){n=n?1+f(n/b,b):0;}  Try it online! For input positive integers $$\n\$$ and $$\b\$$ returns the number of digits in $$\n_b\$$. # Java, 30 bytes n->b->n.toString(n,b).length()  Try it online! • Doesn't n->b->n.toString(b).length() work? – Neil Commented Nov 5, 2021 at 20:41 • @Neil No, because Integer.toString is a static method, not an instance method. Commented Nov 5, 2021 at 21:04 • Ah, so n.toString() is an instance method, but there's no equivalent with a radix, and n.toString(b) just invokes Integer.toString(b). – Neil Commented Nov 5, 2021 at 22:35 • @Neil Yes, that's it. Commented Nov 5, 2021 at 22:51 # Rust, 40 bytes |n,b:u8|(n as f64).log(b.into())as u64+1  Try it online! Unfortunately the typecasts are neccessary because the int_log feature is not yet stabilized. With the feature the code could be shortened to |n:u64,b|n.log(b)+1  # ErrLess, 27 bytes 0m:1$@;<2+]{@;/$;$!0"+0}!.M


## Explanation

A macro.

Note: ErrLess doesn't support floating point numbers, so all division is integer division.

0m {...} M { Define a macro identified by 0 }

:            { Wrap input in a stack: ((b n)) }
1${ Add one to the stack, before input, used later: (1 (b n)) } @;< { b < n? } 2+]{ {...} } { If not so, } @;/ { (1 (b n) b/n) }$;$! { (1 b/n b) } 0" { Recurse (yes, I'm using " to call the macro...) } + { Add result to the previously added one } 0 { Add dummy value to stack } ! { Pop top value of stack, either dummy value or unused arguments, depending on if above code executed } . { Halt (return), basically returns either 1 or 1+recurse(b/n, n) }  Try it online! ## dc, 2320 17 bytes [lb/dd0<g]dsgxz1-  Takes input with the base in register b and the number at the top of the stack. The answer is at the top of the stack once the program completes. Basically, it recursively calculates the number of digits, storing the current count as the second-topmost value in the stack and the current number at the top of the stack. What I did before was pretty long, and it turns out it's shorter to just add new elements when you delete each digit of n in base b, and use the stack's size instead of a register. Run from the command-line as follows: dc -e '{BASE}sb{NUMBER} [lb/dd0<g]dsgxz1- p'  where {BASE} should be replaced by the base and {NUMBER} should be replaced by the number. Pretty self-explanatory. ### Explanation [lb/dd0<g]dsgxz1- [ ]dsgx # Store the text inside the [] in register g and run it lb/ # Divide the current number by the base (in register b) dd # Duplicate it twice, once to store the number of digits, # and another for the comparison with 0 0<g # Run the string (function) again if it's more than zero z1- # Find the size of the stack and subtract one  The 1- at the end seems wasteful, but I don't know of a way to reduce it. PS: This is my first dc program, so any feedback would be appreciated. Also, I'm not sure if this is the best site for dc, would the esolangs one be better? It has less information and wasn't as helpful for me though. # Excel, 17 bytes =LEN(BASE(A1,B1))  Link to Spreadsheet Counting digits in base conversion beats using log by 1 byte ### Log Method, 18 bytes =INT(LOG(A1,B1))+1  • I believe you can remove the trailing parentheses on the first one, and, if you change it to 1+ at the start, you can do the same on the second one. Commented Nov 7, 2021 at 13:45 • I've seen answers where the trailing parentheses are removed from the byte count. I choose to to leave them in since Excel puts them in after you hit enter and technichally the formula requires them to work. Commented Nov 7, 2021 at 14:03 # PHP, 22 bytes fn($i)=>1+log(...$i)|0  Try it online! Or the super boring version : ### PHP, 41 bytes fn($n,$b)=>strlen(base_convert($n,10,$b))  Try it online! # PowerShell Core, 42 bytes param($a,$b)for(;$a-gt$b;$i++){$a/=$b}\$i+1


Try it online!

# Go, 65 bytes, using math

import."math"
func f(n,b float64)int{return int(Log(n)/Log(b)+1)}


Attempt This Online!

# Go, 69 bytes, using strconv

import."strconv"
func f(n int64,b int)int{return len(FormatInt(n,b))}


Attempt This Online!

nice wow builtins