25
\$\begingroup\$

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?

\$\endgroup\$
1
  • 7
    \$\begingroup\$ This is this but extended to any number. \$\endgroup\$ Commented Apr 23, 2021 at 21:38

36 Answers 36

12
\$\begingroup\$

Vyxal, 1 byte

Ǒ 

Try it Online!

laughs in stolen jelly built-ins

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 6 bytes with no builtin \$\endgroup\$ Commented Jun 15, 2021 at 20:36
12
\$\begingroup\$

K (ngn/k), 11 8 7 bytes

*&|(\). 

Try it online!

Takes input as a list of two integers, the first b, the second n, e.g. 2 512.

  • (\). convert n to base b
  • | reverse
  • & use monadic where, e.g. convert 0 0 2 into 2 2
  • * return the first value
\$\endgroup\$
9
\$\begingroup\$

Jelly, 1 byte

Try it online!

Don't know Jelly

\$\endgroup\$
2
  • 1
    \$\begingroup\$ "Don't know Jelly" - how you find it, then ??!!?!? \$\endgroup\$ Commented Apr 25, 2021 at 21:21
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ Commented Apr 26, 2021 at 2:08
9
\$\begingroup\$

Python 2, 31 bytes

Returns False for 0.

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

Try it online!

\$\endgroup\$
8
\$\begingroup\$

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

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

Python 3, 83 78 57 47 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

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

JavaScript (V8), 22 bytes

-3 thanks to @user

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

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Save 3 bytes with currying (d=>g=x=>x%d?0:g(x/d)+1) \$\endgroup\$ Commented Apr 23, 2021 at 21:56
6
\$\begingroup\$

MATL, 11 8 bytes

_YAPf1)q 

Inputs are in reverse order.

Try it online! Or verify all test cases.

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

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

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.

\$\endgroup\$
5
\$\begingroup\$

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.

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

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Recursion here pays well Try it online! \$\endgroup\$ Commented Apr 24, 2021 at 9:21
  • 2
    \$\begingroup\$ @AZTECCO strikes again!! Thanks :D \$\endgroup\$ Commented Apr 24, 2021 at 9:29
4
\$\begingroup\$

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).
\$\endgroup\$
2
  • 2
    \$\begingroup\$ 12b: [:#.~0=#.inv \$\endgroup\$ Commented Apr 24, 2021 at 9:40
  • \$\begingroup\$ That's a fantastic trick. \$\endgroup\$ Commented Apr 24, 2021 at 13:19
4
\$\begingroup\$

Pari/GP, 9 bytes

valuation 

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is an equally valid answer. \$\endgroup\$ Commented Apr 26, 2021 at 2:32
  • \$\begingroup\$ I don't know how the rules work for inbuilt functions. Wouldn't it have to be at least "valuation(n,b)"? \$\endgroup\$ Commented Apr 26, 2021 at 4:36
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Apr 26, 2021 at 4:49
  • \$\begingroup\$ Well, that shrunk it a lot. Thanks! \$\endgroup\$ Commented Apr 26, 2021 at 8:42
3
\$\begingroup\$

Wolfram Language (Mathematica), 15 bytes

IntegerExponent 

Try it online!

Built-in.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 11 bytes

IL⊟⪪⭆↨NN¬ι0 

Try it online! Link is to verbose version of code. Explanation:

 N Input `n` ↨ Converted to base N Input `b` ⭆ Map over elements and join ι Current element ¬ Logical Not ⪪ 0 Split string on literal `0` ⊟ Take the last string of `1`s L Take the length I Cast to string Implicitly print 
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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

R, 38 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Red, 48 bytes

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

Try it online!

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

Julia, 18 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

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.

\$\endgroup\$
3
\$\begingroup\$

Hy, 40 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Ruby, 27 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 5 bytes

вRĀ1k 

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

Try it online or verify all test cases.

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

Haskell, 32 bytes

a?b|a`mod`b>0=0|c<-a`div`b=1+c?b 

Try it online!

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

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.

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

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

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Apr 26, 2021 at 11:30
  • \$\begingroup\$ @cairdcoinheringaahing ofc! I added it, hope it's all correct now. \$\endgroup\$ Commented Apr 26, 2021 at 11:34
2
\$\begingroup\$

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.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.