7
\$\begingroup\$

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
\$\endgroup\$
3
  • 3
    \$\begingroup\$ consider adding test case with 0 like 0 2 -> 1? a lot of them seem to fail with it \$\endgroup\$
    – Hydrazer
    Nov 4 '21 at 13:19
  • 7
    \$\begingroup\$ @Hydrazer But 0 is not a positive integer? \$\endgroup\$
    – ovs
    Nov 4 '21 at 14:29
  • \$\begingroup\$ oh i guess you're right \$\endgroup\$
    – Hydrazer
    Nov 4 '21 at 21:32

27 Answers 27

7
\$\begingroup\$

JavaScript (V8), 21 bytes

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

Try it online!

\$\endgroup\$
7
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You could switch to Python 2 so the // can be /. \$\endgroup\$ Nov 4 '21 at 12:38
  • 2
    \$\begingroup\$ i think you need to include the function name if you are doing recursion \$\endgroup\$
    – Hydrazer
    Nov 4 '21 at 13:12
6
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ Could you use ceiling instead of increment and floor? \$\endgroup\$
    – Graham
    Nov 4 '21 at 11:40
  • 1
    \$\begingroup\$ @Graham No, because log₁₀1000 is 3 but 1000 takes 4 digits. \$\endgroup\$
    – Adám
    Nov 4 '21 at 11:41
  • \$\begingroup\$ How is ≢⊤ calculated as "2 bytes"? I.e. what encoding is used? \$\endgroup\$ Nov 6 '21 at 10:43
  • 1
    \$\begingroup\$ @Feuermurmel I've updated the post heading to explain. \$\endgroup\$
    – Adám
    Nov 6 '21 at 18:46
5
+50
\$\begingroup\$

Zsh, 20 bytes

My first Zsh answer, cobbled together using these very helpful posts.

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

Try it online or run the test suite.

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!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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) \$\endgroup\$
    – pxeger
    Nov 5 '21 at 8:01
4
\$\begingroup\$

Charcoal, 5 bytes

IL↨NN

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:

   N    Input `n` as an integer
    N   Input `b` as an integer
  ↨     Convert `n` to base `b` as an array
 L      Take the length
I       Cast to string
        Implicitly print
\$\endgroup\$
1
  • \$\begingroup\$ 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). \$\endgroup\$
    – Neil
    Nov 4 '21 at 17:07
4
\$\begingroup\$

Japt, 4 bytes

sV l

Try it here

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 2 bytes

вg

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

Try it online or verify all test cases.

Using logarithm like most other answers is 4 bytes:

.n>ï

Try it online or verify all test cases.

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

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ ljF saves a byte if you take input in the form [a,b] (or even a,b) \$\endgroup\$
    – Mr. Xcoder
    Nov 7 '21 at 7:59
3
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ How is #2 15 bytes? \$\endgroup\$
    – emanresu A
    Nov 5 '21 at 3:33
  • 1
    \$\begingroup\$ @emanresuA ⌊⌋ are each 3 bytes. \$\endgroup\$
    – att
    Nov 5 '21 at 3:53
3
\$\begingroup\$

Vyxal, 2 bytes

τL

Try it Online!

Simplest method

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

\$\endgroup\$
1
  • 2
    \$\begingroup\$ rounds, so #2 is wrong \$\endgroup\$
    – emanresu A
    Nov 5 '21 at 3:35
2
\$\begingroup\$

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!

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

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!

\$\endgroup\$
2
\$\begingroup\$

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

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!

\$\endgroup\$
2
\$\begingroup\$

Jelly, 2 bytes

bL

Try it online!

Finally, a Jelly answer using the standard English keyboard.

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

Ruby, 21 bytes

->n,b{/$/=~n.to_s(b)}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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

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

Java, 30 bytes

n->b->n.toString(n,b).length()

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Doesn't n->b->n.toString(b).length() work? \$\endgroup\$
    – Neil
    Nov 5 '21 at 20:41
  • \$\begingroup\$ @Neil No, because Integer.toString is a static method, not an instance method. \$\endgroup\$ Nov 5 '21 at 21:04
  • \$\begingroup\$ 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). \$\endgroup\$
    – Neil
    Nov 5 '21 at 22:35
  • \$\begingroup\$ @Neil Yes, that's it. \$\endgroup\$ Nov 5 '21 at 22:51
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

ErrLess, 27 bytes

0m:1$@;<2+]{@;/$;$!0"+0}!.M

Explanation

Basically copied this JS answer.

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!

\$\endgroup\$
1
\$\begingroup\$

dc, 23 20 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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – pxeger
    Nov 7 '21 at 13:45
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Axuary
    Nov 7 '21 at 14:03
1
\$\begingroup\$

Desmos, 23 bytes

f(n,b)=floor(\log_bn+1)

Try It On Desmos!

Try It On Desmos! - Prettified

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

Core Maude, 107 bytes

mod B is pr NAT . op f : Nat Nat -> Nat . vars N B : NzNat . eq f(N,B)= s f(N
quo B,B). eq f(0,B)= 0 . endm

The answer is obtained by reducing the f function with \$n\$ and \$b\$.

Example Session

             \||||||||||||||||||/
           --- Welcome to Maude ---
             /||||||||||||||||||\
         Maude 3.1 built: Oct 12 2020 20:12:31
         Copyright 1997-2020 SRI International
           Thu Nov  4 21:22:07 2021
Maude> mod B is pr NAT . op f : Nat Nat -> Nat . vars N B : NzNat . eq f(N,B)= s f(N
> quo B,B). eq f(0,B)= 0 . endm
Maude> red f(1597, 16) .  --- Expected: 3
result NzNat: 3
Maude> red f(1709, 9) .  --- Expected: 4
result NzNat: 4
Maude> red f(190, 29) .  --- Expected: 2
result NzNat: 2
Maude> red f(873, 24) .  --- Expected: 3
result NzNat: 3
Maude> red f(1061, 27) .  --- Expected: 3
result NzNat: 3
Maude> red f(289, 26) .  --- Expected: 2
result NzNat: 2
Maude> red f(1575, 34) .  --- Expected: 3
result NzNat: 3
Maude> red f(1135, 15) .  --- Expected: 3
result NzNat: 3
Maude> red f(1161, 22) .  --- Expected: 3
result NzNat: 3
Maude> red f(585, 23) .  --- Expected: 3
result NzNat: 3
Maude> red f(1412, 23) .  --- Expected: 3
result NzNat: 3
Maude> red f(1268, 14) .  --- Expected: 3
result NzNat: 3
Maude> red f(714, 12) .  --- Expected: 3
result NzNat: 3
Maude> red f(700, 29) .  --- Expected: 2
result NzNat: 2
Maude> red f(1007, 35) .  --- Expected: 2
result NzNat: 2
Maude> red f(292, 17) .  --- Expected: 3
result NzNat: 3
Maude> red f(1990, 16) .  --- Expected: 3
result NzNat: 3
Maude> red f(439, 3) .  --- Expected: 6
result NzNat: 6
Maude> red f(1212, 17) .  --- Expected: 3
result NzNat: 3
Maude> red f(683, 31) .  --- Expected: 2
result NzNat: 2
Maude> red f(535, 25) .  --- Expected: 2
result NzNat: 2
Maude> red f(1978, 32) .  --- Expected: 3
result NzNat: 3
Maude> red f(153, 8) .  --- Expected: 3
result NzNat: 3
Maude> red f(1314, 33) .  --- Expected: 3
result NzNat: 3
Maude> red f(433, 2) .  --- Expected: 9
result NzNat: 9
Maude> red f(655, 35) .  --- Expected: 2
result NzNat: 2
Maude> red f(865, 19) .  --- Expected: 3
result NzNat: 3
Maude> red f(1947, 25) .  --- Expected: 3
result NzNat: 3
Maude> red f(1873, 32) .  --- Expected: 3
result NzNat: 3
Maude> red f(1441, 12) .  --- Expected: 3
result NzNat: 3
Maude> red f(228, 30) .  --- Expected: 2
result NzNat: 2
Maude> red f(947, 2) .  --- Expected: 10
result NzNat: 10
Maude> red f(1026, 11) .  --- Expected: 3
result NzNat: 3
Maude> red f(1172, 24) .  --- Expected: 3
result NzNat: 3
Maude> red f(1390, 32) .  --- Expected: 3
result NzNat: 3
Maude> red f(1495, 21) .  --- Expected: 3
result NzNat: 3
Maude> red f(1339, 10) .  --- Expected: 4
result NzNat: 4
Maude> red f(1357, 9) .  --- Expected: 4
result NzNat: 4
Maude> red f(1320, 27) .  --- Expected: 3
result NzNat: 3
Maude> red f(602, 5) .  --- Expected: 4
result NzNat: 4
Maude> red f(1462, 16) .  --- Expected: 3
result NzNat: 3
Maude> red f(1658, 9) .  --- Expected: 4
result NzNat: 4
Maude> red f(519, 11) .  --- Expected: 3
result NzNat: 3
Maude> red f(159, 3) .  --- Expected: 5
result NzNat: 5
Maude> red f(1152, 11) .  --- Expected: 3
result NzNat: 3
Maude> red f(1169, 33) .  --- Expected: 3
result NzNat: 3
Maude> red f(1298, 7) .  --- Expected: 4
result NzNat: 4
Maude> red f(1686, 32) .  --- Expected: 3
result NzNat: 3
Maude> red f(1227, 25) .  --- Expected: 3
result NzNat: 3
Maude> red f(770, 15) .  --- Expected: 3
result NzNat: 3
Maude> red f(1478, 20) .  --- Expected: 3
result NzNat: 3
Maude> red f(401, 22) .  --- Expected: 2
result NzNat: 2
Maude> red f(1097, 7) .  --- Expected: 4
result NzNat: 4
Maude> red f(1017, 9) .  --- Expected: 4
result NzNat: 4
Maude> red f(784, 23) .  --- Expected: 3
result NzNat: 3
Maude> red f(1176, 15) .  --- Expected: 3
result NzNat: 3
Maude> red f(1521, 7) .  --- Expected: 4
result NzNat: 4
Maude> red f(1623, 23) .  --- Expected: 3
result NzNat: 3
Maude> red f(1552, 13) .  --- Expected: 3
result NzNat: 3
Maude> red f(819, 15) .  --- Expected: 3
result NzNat: 3
Maude> red f(272, 32) .  --- Expected: 2
result NzNat: 2
Maude> red f(1546, 12) .  --- Expected: 3
result NzNat: 3
Maude> red f(1718, 4) .  --- Expected: 6
result NzNat: 6
Maude> red f(1686, 8) .  --- Expected: 4
result NzNat: 4
Maude> red f(1128, 2) .  --- Expected: 11
result NzNat: 11
Maude> red f(1617, 34) .  --- Expected: 3
result NzNat: 3
Maude> red f(1199, 34) .  --- Expected: 3
result NzNat: 3
Maude> red f(626, 23) .  --- Expected: 3
result NzNat: 3
Maude> red f(991, 9) .  --- Expected: 4
result NzNat: 4
Maude> red f(742, 22) .  --- Expected: 3
result NzNat: 3
Maude> red f(1227, 11) .  --- Expected: 3
result NzNat: 3
Maude> red f(1897, 12) .  --- Expected: 4
result NzNat: 4
Maude> red f(348, 35) .  --- Expected: 2
result NzNat: 2
Maude> red f(1107, 11) .  --- Expected: 3
result NzNat: 3
Maude> red f(31, 26) .  --- Expected: 2
result NzNat: 2
Maude> red f(1153, 26) .  --- Expected: 3
result NzNat: 3
Maude> red f(432, 4) .  --- Expected: 5
result NzNat: 5
Maude> red f(758, 9) .  --- Expected: 4
result NzNat: 4
Maude> red f(277, 21) .  --- Expected: 2
result NzNat: 2
Maude> red f(472, 29) .  --- Expected: 2
result NzNat: 2
Maude> red f(1935, 21) .  --- Expected: 3
result NzNat: 3
Maude> red f(457, 27) .  --- Expected: 2
result NzNat: 2
Maude> red f(1807, 26) .  --- Expected: 3
result NzNat: 3
Maude> red f(1924, 26) .  --- Expected: 3
result NzNat: 3
Maude> red f(23, 27) .  --- Expected: 1
result NzNat: 1
Maude> red f(558, 30) .  --- Expected: 2
result NzNat: 2
Maude> red f(203, 15) .  --- Expected: 2
result NzNat: 2
Maude> red f(1633, 8) .  --- Expected: 4
result NzNat: 4
Maude> red f(769, 21) .  --- Expected: 3
result NzNat: 3
Maude> red f(1261, 32) .  --- Expected: 3
result NzNat: 3
Maude> red f(577, 7) .  --- Expected: 4
result NzNat: 4
Maude> red f(1440, 22) .  --- Expected: 3
result NzNat: 3
Maude> red f(1215, 35) .  --- Expected: 2
result NzNat: 2
Maude> red f(1859, 23) .  --- Expected: 3
result NzNat: 3
Maude> red f(1702, 35) .  --- Expected: 3
result NzNat: 3
Maude> red f(1580, 12) .  --- Expected: 3
result NzNat: 3
Maude> red f(782, 15) .  --- Expected: 3
result NzNat: 3
Maude> red f(701, 32) .  --- Expected: 2
result NzNat: 2
Maude> red f(177, 24) .  --- Expected: 2
result NzNat: 2
Maude> red f(1509, 28) .  --- Expected: 3
result NzNat: 3

Ungolfed

mod B is
    pr NAT .
    op f : Nat Nat -> Nat .
    vars N B : NzNat .
    eq f(N, B) = s f(N quo B, B) .
    eq f(0, B) = 0 .
endm

Maude does have logarithms, but only for floats and only the natural logarithm. To get the number of digits we'd need to compute \$\mathrm{ceiling}(\log(n) / \log(b))\$, which actually winds up being a few bytes longer than just doing repeated division.

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

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!

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