New answers tagged

1

Python 3, 77 bytes Returns in list. def f(x,b,s=[],n=0): while x:s,x,n=[x%b[n]]+s,x//b[n],(n+1)%len(b) return s Try it online! while x: Keeps self-updating while X is larger than the selected element in [base].


1

C (gcc), 70 bytes a;i;r;f(x,b,l)int*b;{for(a=1,r=i=0;x;x/=10)r+=x%10*a,a*=b[i++%l];x=r;} Try it online! Inputs a non-negative integer in base 10, a pointer to the array of bases, and the array's length (since C pointers don't carry any length info).


4

Haskell, 32 bytes (x:y)?(h:t)=x+h*y?(t++[h]) _?_=0 Try it online! The relevant function is (?), which takes as input X as a list of digits (least significant first) and the list of bases. Haskell, 35 bytes (foldr(\(i,d)n->d+i*n)0.).zip.cycle Try it online! Same algorithm in point-free style. Takes the list of digits first and then X.


2

Charcoal, 13 bytes IΣE⮌θΠ⊞O…ηκIι Try it online! Link is to verbose version of code. Takes X as a string. Explanation: θ First input (X) ⮌ Reversed E Map over characters ι Current character I Cast to integer ⊞O Push to list η Second input (list of bases) …...


2

Retina, 90 bytes $ ; +`(\d+,)(.*,)?(\d+)(\d); $2$1$3;$4 +`(.*,)?(\d+,)(\d+);(\d) $2$1$.($2*$3*_$4*); .*,|; Try it online! Link includes test cases. Takes X as the last argument. Explanation: $ ; Append a marker to keep track of the current digit. +`(\d+,)(.*,)?(\d+)(\d); $2$1$3;$4 Cyclically rotate the list of bases according to the number of digits. +`(.*...


6

Jelly, 7 bytes Takes the bases in reverse order as the first argument and the digits as the second argument. ṁżṛḅ@ƒ0 Try it online! Explanation ṁżṛḅ@ƒ0 Main dyadic link ṁ Reshape the list of bases like the list of digits ż Zip with ṛ the list of digits ƒ0 Reduce with initial value 0 under: ḅ Convert from base ...


1

JavaScript (V8), 45 bytes (b,i)=>g=X=>X&&X%10+b[i=b[++i]?i:0]*g(X/10|0) Try it online!


0

Pari/GP, 68 bytes Solved via recursion: we keep storing the modulo, dividing by the base, and drilling down until we hit zero. Then we print each digit on the way back up. f(n,v)=my(m=n%v[1]);n\=v[1];n&&f(n,vector(#v,i,v[i%#v+1]));print1(m) Try it online!


7

APL(Dyalog Unicode), 6 bytes SBCS ⍴⍨∘≢⊥⊢ Try it on APLgolf! An anonymous tacit infix function taking the base in reverse as left argument and X as a list of digits as right argument. ⊢ X ⊥ evaluated in base… ⍴⍨∘≢ base cyclically reshaped to the length of X


3

Husk, 9 8 bytes δṁ*G*1t¢ Try it online! Same idea as hyper-neutrino's answer. -1 byte from Leo.


2

JavaScript (V8), 60 55 52 bytes n=>b=>(g=r=>x in n?g(n[x]+r*b[++x%b.length]):r)(x=0) Try it online!


4

Vyxal s, 8 bytes •Ḣ1p⁽*r* Try it Online! •Ḣ1p⁽*r* Full Program • Mold bases like digits Ḣ Pop first base off 1p Prepend 1 r Cumulative reduce over ⁽* Multiplication * Vectorized multiplication with digits s (flag) Sum


5

Pyth, 16 bytes JEs*L*F<t*JlQ~hZ Test suite Inspired quite heavily by hyper-neutrino's Jelly answer. Takes reversed list of digits of X on line 1 and reversed list of bases on line 2 of input. Explanation: JEs*L*F<t*JlQ~hZ | Full program JEs*L*F<t*JlQ~hZQ | with implicit variables ------------------+--------------------------------------------------...


6

Jelly, 7 bytes ṁḊ1;×\ḋ Try it online! -1 byte thanks to Jonathan Allan ṁ Mold bases to the shape/length of the digits, repeating if necessary Ḋ Drop the first base 1; and prepend 1 in its place ×\ Cumulatively reduce the bases by multiplication ḋ Dot product with the digits


0

Husk, 5 bytes mḋġ≠ḋ Try it online!


1

Retina 0.8.2, 65 bytes \d+ $*_ +`^(_+),(?>(.*,)?)(\1)+(_*) $2$1,$#3$*_$.4 (_+,)+(_*) $.2 Try it online! Link includes test cases. Takes X as the last argument. Explanation: \d+ $*_ Convert to unary. +`^(_+),(?>(.*,)?)(\1)+(_*) Repeat until X is less than the first base, divmod X by that, and... $2$1,$#3$*_$.4 ... move the first base to the end, ...


1

Wolfram Language (Mathematica), 38 39 bytes #>=#2&&#0[⌊#/#2⌋,##3,#2]||#~Mod~#2& Try it online! Input [X, bases..]. Returns an Or of digits. MixedRadix exists and would perform the task easily (in conjunction with IntegerDigits), but it isn't cyclic.


1

C (gcc), 82 bytes a;c;i;r;f(x,b,l)int*b;{for(a=1,r=i=0;x;x/=c)c=b[i++%l],r+=x%c*a,a*=10,x-=x%c;x=r;} Try it online! Inputs a non-negative integer in base \$10\$, a pointer to the array of bases, and the array's length (since C pointers don't carry any length info).


2

MathGolf, 15 bytes ê├Ä_]─k\É‼%/]xy First input is \$X\$, all other inputs are the values in the bases-list. Try it online. Explanation: ê # Push all inputs as an integer-list ├ # Pop its first item (the integer X), and push it to the stack Ä # Loop that many times, using a single character as inner code-block: _ # Duplicate ...


1

R, 66 59 58 55 bytes Edit: -7 bytes thanks to att's comment on dingledooper's answer: previous version uselessly worried about handling bases >10 f=function(n,b)`if`(n,10*f(n%/%b,c(b[-1],b))+n%%b,0)[1] Try it online!


3

05AB1E, 11 bytes sиvy‰`s}JRï I have the feeling this can be shorter, but I'm a bit rusty on code-golfing. Takes the list of bases as first input and integer \$X\$ as second input. Try it online or verify all test cases. Explanation: s # Swap the two (implicit) inputs, so the stack is bases-list, integer X и # Repeat the bases-list X amount of ...


0

Charcoal, 20 bytes F¬θ0WθFη¿θ«←I﹪θκ≧÷κθ Try it online! Link is to verbose version of code. Explanation: F¬θ0 Special-case an input of 0. WθFη¿θ« While the input is non-zero, loop over all the bases while the input is non-zero. ←I﹪θκ Output the next digit in reverse order. ≧÷κθ Divide by the base.


2

Haskell, 41 bytes 0?_=0 n?(h:t)=mod n h+10*div n h?(t++[h]) Try it online!


3

Python 3, 41 bytes A massive improvement from my previous solution, based on a clever observation by @att, to use the fact that bases are \$ \le 10 \$. f=lambda x,y,*b:x and 10*f(x//y,*b,y)+x%y Try it online! Python 3, 48 bytes Outputs the number as a list of digits. A couple of bytes are needed to handle the edge cases where \$ X = 0 \$. f=lambda x,y,*b:...


1

Pyth, 23 bytes M|&J/GhHagJ.<H1%GhH]GgF Test suite Direct translation of dingledooper's Python 3 answer. Pyth, 25 bytes AQWG=+k%G@HZ~/G@H~hZ)|_k0 Test suite Naïve solution. Surprised it's only 2 bytes longer...


6

APL(Dyalog Unicode), 8 bytes SBCS A re-implementation of Jonah's answer. 10⊥⌽⍤⍴⊤⊣ Try it on APLgolf! An anonymous tacit infix function taking base on the right and X on the left. ⊣ the value X ⊤ represented in base… ⌽⍤⍴ reversed base cyclically-repeated until length X 10⊥ evaluated as base-10


2

JavaScript (ES6), 43 bytes f=(n,[b,...a])=>n&&+f(n/b|0,[...a,b])+[n%b] Try it online!


4

J, 12 11 bytes 10#.|.@$#:[ Try it online! -1 after reading Adam's APL answer and realizing I could get rid of the ~. Other than that, we both found the exact same answer. J has a mixed base primitive that does the heavy lifting #:. |.@$~ Repeat the base as man times as the number we're converting, and then reverse. Extra left digits will become 0, and ...


4

Stax, 13 11 10 bytes ╨¬ª₧}ÄM☻☻. Run and debug it Having another stack based language to compete with gave some golfing motivation. Explanation n^*{|%mr$A|b n^* repeat bases X+1 times { m map to |% divmod(puts quotient on stack for next iteration) r reverse $ join to string ...


2

Python 3, 57 bytes f=lambda x,a,*b,j=0:x and f(x//a,*b[1:],a,j=[])+[x%a]or j Try it online! -5 bytes borrowing the idea from Arnauld to use splat.


1

Factor, 38 bytes [ 37 iota [ base> ] with map sift Σ ] Try it online! Explanation: It's a quotation (anonymous function) that takes a string from the data stack as input and leaves an integer on the data stack as output. 37 iota Create a range from 0 to 36 inclusive. [ base> ] with map Convert the input to base 10 from each base between 0 and 36. If ...


1

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


2

C# (Visual C# Interactive Compiler), 29 bytes f=(n,b)=>n=n%b>0?0:1+f(n/b,b) Try it online!


2

Ruby, 27 bytes f=->n,b{n%b>0?0:1+f[n/b,b]} Try it online!


4

Pari/GP, 9 bytes valuation Try it online!


2

Hy, 40 bytes (defn f[n b](if(% n b)0(+(f(/ n b)b)1))) Try it online!


2

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.


2

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.


2

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


1

Haskell, 32 bytes a?b|a`mod`b>0=0|c<-a`div`b=1+c?b Try it online!


2

Julia, 18 bytes n*b=n%b<1&&1+n/b*b Try it online!


2

GAWK, 32 bytes {for(;$1/$2^++e!~/\./;);$0=e-1}1 Strangely, does not work on TIO, and also couldn't run it in mawk. {for(; starts the loop $1/$2^++e divides n by b^(e), e increasing each time !~/\./ continue while this division does not match a dot (i.e, while it's a integer) ;); ...


3

Red, 48 bytes f: func[n b][either(n % b)> 0[0][1 + f n / b b]] Try it online!


3

R, 38 bytes f=function(n,b)`if`(n%%b,0,1+f(n/b,b)) Try it online!


3

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


4

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


6

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


4

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!


8

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


3

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.


Top 50 recent answers are included