# Count the trailing zeros

Given two positive integers $$\n\$$ and $$\b\$$, such that $$\n > 2\$$ and $$\n > b > 1\$$, count the trailing zeros when converting $$\n\$$ to base $$\b\$$. Your program should be able to handle any value of $$\b\$$ within your integer maximum.

The digits of $$\n = 18\$$, $$\b = 3\$$ are $$\[2,0,0]\$$, so the correct output is $$\2\$$.
The digits of $$\n = 107\$$, $$\b = 43\$$ are $$\[2, 42]\$$, so the correct output is $$\0\$$

This is , so the shortest code in bytes wins

## Test cases

  n   b  out
512   2   9
248   2   3
364 265   0
764   2   2
336 284   0
517 422   0
554  37   0
972   3   5
12   6   1
72   2   3
44   2   2
51  16   0
32   2   5
56   7   1
60   2   2
8   3   0
18   3   2
107  43   0


Credit to Anush for the inspiration

There is a one byte answer in Jelly. Can you find it?

• This is this but extended to any number. Commented Apr 23, 2021 at 21:38

# Vyxal, 1 byte

Ǒ


Try it Online!

laughs in stolen jelly built-ins

• 6 bytes with no builtin Commented Jun 15, 2021 at 20:36

# K (ngn/k), 11 8 bytes

{*&|y\x}


Try it online!

• |y\x convert x to base y, reversing the resulting digits
• & use monadic where, e.g. convert 0 0 2 into 2 2
• * return the first value

# Python 2, 31 bytes

Returns False for 0.

f=lambda n,b:n%b<1and-~f(n/b,b)


Try it online!

# Jelly, 1 byte

ọ


Try it online!

Don't know Jelly

• "Don't know Jelly" - how you find it, then ??!!?!? Commented Apr 25, 2021 at 21:21
• @Programmer I'm halfway through making my own golfing language, so I know a bit about the syntax and I managed to pick up what this operator does from other answers :p Commented Apr 26, 2021 at 2:08

# .NET Regex, 35 bytes

\G(?=1+,1(1+))(?=(1\1)+,)(?<-2>\1)+


Try it online! Test suite written using Retina. Takes n,b as input in unary as strings of 1s and outputs the result as the number of successful matches. Explanation:

\G


Each match must begin at the end of the previous match (with the first match beginning at the start of the string).

(?=1+,1(1+))


Look ahead to find b and capture b-1 as \1.

(?=(1\1)+,)


Ensure that 1\1, i.e. b, divides n.

(?<-2>\1)+


Subtract b-1 times the quotient, leaving the quotient, i.e. n/b. This value is then the input to the next match.

# Python 3, 83785747 40 bytes

f=lambda n,b:1+f(n/b,b)if n>n%b<=0else 0


Finally figured out how to use a lambda to solve this

• Welcome to Code Golf! Instead of converting to a string, I'd suggest using a counter so you don't need len
– user
Commented Apr 23, 2021 at 22:00
• thanks for the suggestion! Commented Apr 23, 2021 at 22:06

# JavaScript (V8), 22 bytes

-3 thanks to @user

d=>g=x=>x%d?0:g(x/d)+1


Try it online!

# MATL, 11 8 bytes

_YAPf1)q


Inputs are in reverse order.

### Explanation

Consider inputs n = 12321, b = 3 as an example.

_    % Implicit input: b. Negate
% STACK: -3
YA   % Implicit input: n. Convert n to specified base. A negative base -b is
% interpreted as digits 0,1,...,b-1, and b can be arbitrarily large.
% (A positive base b is interpreted as chars '0','1',...,'9','A','B',...,
% and then b cannot exceed 36)
% STACK: [1 2 1 2 2 0 1 0 0]
P    % Flip
% STACK: [0 0 1 0 2 2 1 2 1]
f    % Find: indices of non-zeros
% STACK: [3 5 6 7 8 9]
1)   % First entry
% STACK: 3
q    % Subtract 1. Implicit display
% STACK: 2

• You can count the number of times $b$ appears in the prime factorization (including multiplicities) of $n$. Would that save bytes? Commented Apr 23, 2021 at 21:50
• @cairdcoinheringaahing Thanks. I'm using a different approach because that one seemed longer, but I'm not sure. Maybe someone else can try it :-) Commented Apr 23, 2021 at 21:54

# MMIX, 28 bytes (7 instrs)

35020001 F7010000
1E000001 E7020001
FE030006 5303FFFD
F8030000


# Explanation

    NEG  $2,0,1 // set$2 to -1
PUT  rD,0       // clear hidiv register
lop DIVU $0,$0,$1 // set$0 to $0/$1 and rR to $0%$1
INCL $2,1 // increment$2
GET  $3,rR //$3 = rR
PBZ  $3,lop // if$3, jump back to loop
POP  3,0        // return three registers; $2 goes into lowest slot!  The only tricks I used were to start the register at -1 to simplify the logic, and to use the properties of POP to shift the register while returning. The MMIX register shuffle is useful when golfing. (It's possible to shave an extra tetra by using DIV instead of DIVU and omitting the PUT, but this changes the semantics to be signed instead of unsigned. For completeness, this leads to the following machine code.) 35020001 1C000001 E7020001 FE030006 5303FFFD F8030000  # Raku, 22 bytes {($^n,*/$^x.../\./)-2}  Try it online! $^n, * / $^x ... /\./ is a sequence starting with $^n (the first argument to the function), where each term is obtained by dividing the previous one by $^x (the second argument to the function), and ending when a term matches the regex /\./ (ie, a period). Subtracting 2 from this sequence treats it as a number equal to its length. # Risky, 8 bytes 0+02?}?0?+1*?:0  Try it online! ## Explanation 0 0 + + 0 0 2 prepend to ? first input } base convert ? second input 0 reduce ? accumulator + + 1 1 * × ? item : = 0 0  This problem translates really nicely to a Risky program. I had to add only two filler characters to make the tree symmetrical. # C (gcc), 38 27 bytes Saved 11 bytes thanks to AZTECCO!!! f(n,b){n=n%b?0:1+f(n/b,b);}  Try it online! • Recursion here pays well Try it online! Commented Apr 24, 2021 at 9:21 • @AZTECCO strikes again!! Thanks :D Commented Apr 24, 2021 at 9:29 # J, 16 12 bytes [:#.~0=#.inv  Try it online! -4 thanks to xash! • #.inv Convert to list of base digits • 0= Converts zeroes to ones, everything else to 0. • [:#.~ Use that number as a mixed base to convert itself to a number -- effectively zeroes out everything to the left of the rightmost 0, summing all the ones (that were originally trailing zeroes). • – xash Commented Apr 24, 2021 at 9:40 • That's a fantastic trick. Commented Apr 24, 2021 at 13:19 # Pari/GP, 9 bytes valuation  Try it online! • This is an equally valid answer. Commented Apr 26, 2021 at 2:32 • I don't know how the rules work for inbuilt functions. Wouldn't it have to be at least "valuation(n,b)"? Commented Apr 26, 2021 at 4:36 • valuation works as a function submission because it is the name of a function which can be called with two arguments, it is reusable (can be called multiple times), and it is reassignable (can be assigned to a new name, as in f=valuation). On the other hand, valuation(n,b) is not a valid submission under our rules because it is a snippet (assumes predefined variables n and b). Commented Apr 26, 2021 at 4:49 • Well, that shrunk it a lot. Thanks! Commented Apr 26, 2021 at 8:42 # Wolfram Language (Mathematica), 15 bytes IntegerExponent  Try it online! Built-in. # Charcoal, 11 bytes ＩＬ⊟⪪⭆↨ＮＮ¬ι0  Try it online! Link is to verbose version of code. Explanation:  Ｎ Input n ↨ Converted to base Ｎ Input b ⭆ Map over elements and join ι Current element ¬ Logical Not ⪪ 0 Split string on literal 0 ⊟ Take the last string of 1s Ｌ Take the length Ｉ Cast to string Implicitly print  # Retina 0.8.2, 34 bytes \d+$*
+%^(1+),(\1)+$¶$1,$#2$*
¶


Try it online! Link includes test cases. Takes input in order b,n. Explanation:

\d+
$*  Convert b and n to unary. +%^(1+),(\1)+$
¶$1,$#2\$*


Repeatedly divide n by b.

¶


Count the number of divisions.

# Brachylog, 9 bytes

ḃ₎,0a₁=kl


Try it online!

  ,0         Append a 0 to
ḃ₎           the digits of n in base b.
a₁       Find the largest suffix
=      all elements of which are equal,
k     remove its last element,
l    and output its length.


The shorter ḃ₎a₁≡ᵛ⁰l cannot handle cases with no trailing zeroes, since a₁ cannot generate an empty suffix of a non-empty list, and if it could ≡ᵛ⁰ would fail on it anyhow.

Without utilizing base conversion:

# Brachylog, 11 bytes

⟨{f↔∋~^}h⟩t


Try it online!

    ∋          An element
f            of the list of factors
⟨{     } ⟩     of n
↔           in descending order
~^ h t    is b raised to the power of the output.


# R, 38 bytes

f=function(n,b)if(n%%b,0,1+f(n/b,b))


Try it online!

# Red, 48 bytes

f: func[n b][either(n % b)> 0[0][1 + f n / b b]]


Try it online!

• Do you have to include the f: in the answer As it's a recursive function? Commented Apr 24, 2021 at 9:29
• @cairdcoinheringaahing Oh yes, definitely! It's not that often that I submit recursive functions in Red :) Commented Apr 24, 2021 at 9:44

# Julia, 18 bytes

n*b=n%b<1&&1+n/b*b


Try it online!

# Japt-g, 5 bytes

ìV Ôð


Try it

ìV Ôð     :Implicit input of integers U=n & V=b
ìV        :Convert U to a base-V digit array
Ô      :Reverse
ð     :0-based indices of truthy (non-zero) elements
:Implicit output of first element


# Emacs Lisp, 45 bytes

(defun f(n b)(if(=(% n b)0)(+(f(/ n b)b)1)0))


Try it online!

It seems that recursive functions doesn't work for Emacs Lisp on TIO.

# Hy, 40 bytes

(defn f[n b](if(% n b)0(+(f(/ n b)b)1)))


Try it online!

# Haskell, 32 bytes

a?b|amodb>0=0|c<-adivb=1+c?b


Try it online!

# Desmos, 64 bytes

c=[0...log_bn]
a=n/b^c
f(n,b)=max(c\left\{a=ceil(a):1,0\right\})


Try It On Desmos!

Try It On Desmos(Prettified)!

Function to test on: $$\f(n,b)\$$

It seems like it only works if you copy/paste one expression at a time, instead of copy/pasting it all at once.

# Ruby, 27 bytes

f=->n,b{n%b>0?0:1+f[n/b,b]}


Try it online!

# C# (Visual C# Interactive Compiler), 29 bytes

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


Try it online!

• Welcome to the site! Normally, it's fine to not count the f= but for functions. However, as you refer to the name of the function in the function, you should include the f= in your byte count, as it's a required part of the definition Commented Apr 26, 2021 at 11:30
• @cairdcoinheringaahing ofc! I added it, hope it's all correct now. Commented Apr 26, 2021 at 11:34

# Excel, 52 bytes

=LET(x,SEQUENCE(LOG(A2,B2)),MAX(x*(MOD(A2,B2^x)=0)))


My first attempt below with built-ins was limited because BASE only works up to 36. As a bonus the second attempt was shorter. Goes to show, built-ins aren't always the answer.

## Initial Attempt, 70 bytes

=LET(x,BASE(A2,B2),q,SEQUENCE(LEN(x)),MAX(q*(RIGHT(x,q)=REPT("0",q))))


## Using LAMBDA from Insider Beta, 41 bytes

=LAMBDA(n,b,IF(MOD(n,b)=0,f(n/b,b)+1,0))


The above is 40 bytes plus 1 byte to set it equal to f in the Name Manager.

# 05AB1E, 5 bytes

вRĀ1k


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

Explanation:

в      # Convert the (implicit) input n to the (implicit) input-base b as list
R     # Reverse this list
Ā    # Truthify each value (0 remains 0; everything else becomes 1)
1k  # And get the first (0-based) index of a 1 in this list
# (after which this index is output implicitly as result)