# Base-ically god

## Story

The god of base is opening heaven’s gate for mortals to join the base party. You, as one of the believers, want to follow their steps to ascend to heaven.

But not everyone can get in.

The god posts this question to let you prove you are worthy of their party.

You will get 2 inputs

• First being X which is a non-negative integer in base 10.
• Second being base which is a list of bases to iterate over.
• bases will always be positive integers which in range of 1~10
• The base will contain at least one not 1 base

You will need to present(print) X in the form of iterating bases.

Example:

X ,[base] -> ans

6 ,[2] -> 110
It has only 1 base in base list, thus it converts number into base2

6 ,[2,4] -> 30
First base is 2, so 6%2=0 is the first digit and (6-0)/2 =3 is forwarded to next base.
Second base is 4, so 3%4=3 is the second digit and there is no remaining.

30 ,[2,3] -> 2100
First two digits come from 30%2=0, 15%3=0, with 15/3=5 is passed to next base,
which loops back to 2 => 5%2 = 1,pass 2=> 2%3 = 2

10 ,[9,2] -> 11
20 ,[3,4,5] -> 122
0 ,[any bases] -> 0


## Rules

• No standard loopholes
• Input, output in any reasonable, convenient format is accepted.

The god requires short code—they will give access to the shortest code in each language. Fulfill their task to ascend!

• can we take X in the array like [X,*bases] ? Commented Apr 30, 2021 at 4:51
• @Razetime Any convenience format is allowed :)
– okie
Commented Apr 30, 2021 at 4:54
• Note: giving all ones for the base will never terminate for nonzero input. (Mixing ones with higher numbers is fine) Commented Apr 30, 2021 at 5:07
• If a base is greater than 10, how should I represent a digit of 10 or higher? Commented Apr 30, 2021 at 5:11
• what is base 1 for? Say, what is expected output for 42, [1]?
– tsh
Commented Apr 30, 2021 at 5:20

# APL(Dyalog Unicode), 8 bytes SBCS

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

• Bah, ninja'd while fixing my answer D: Commented Apr 30, 2021 at 5:33

# 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 this ensures we always have enough digits.
• #:] Using the previous result, convert the input using mixed base.
• 10#. Convert back to a base 10 number to remove the extra zeros.
• mixed base conversion ftw Commented Apr 30, 2021 at 5:14
• You can take the input in reverse, so |.@ is not necessary. Commented Apr 30, 2021 at 5:26
• @Bubbler If you look at my edit history, you'll see I tried that but it doesn't always work :) Eg, this test case 20 ,[3,4,5] -> 122 Commented Apr 30, 2021 at 5:27
• @Jonah Oh, didn't notice that. Commented Apr 30, 2021 at 5:28

# 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
A|b      interpret as base 10


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

Explanation:

s        # Swap the two (implicit) inputs, so the stack is bases-list, integer X
и       # Repeat the bases-list X amount of times
v      # Loop over each value y in this list:
y‰    #  Divmod y ([v//y, v%y]), which will use the second (implicit) input-integer
#  X as value in the very first iteration
#  Pop this list, and push both separated to the stack
s  #  Swap their order, so the v//y is at the top, as value for the next iteration
}J     # After the loop: join all integers on the stack together
R    # Reverse it
ï   # And cast it to an integer to remove any leading 0s
# (after which the result is output implicitly)

• Welcome back! I hope all is well. Commented Apr 30, 2021 at 11:41
• @Razetime Hi there. :) Thanks, but I'm not back full-time. I used to code-golf A LOT during work when I was waiting for copies; compilations; build runs; installations; etc., or because I simply was fed up with the current bug/feature I was working on. But since I switched jobs two months ago I barely have those kind of waiting times in between work anymore since everything here is a lot more efficient and thus faster. So you won't see me as often as before for sure, but I might answer a challenge here and there when I feel like it. :) Commented Apr 30, 2021 at 11:45

# 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:x//y and f(x//y,*b,y)+[x%y]or[x]


Try it online!

• 46 bytes with the guarantee that bases are <=10
– att
Commented May 1, 2021 at 4:28
• @att Clever, thanks! I think, because the return value is an integer, we can now ignore the edge case and save 5 more bytes. Commented May 1, 2021 at 4:57

# JavaScript (ES6), 43 bytes

f=(n,[b,...a])=>n&&+f(n/b|0,[...a,b])+[n%b]


Try it online!

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

0?_=0
n?(h:t)=mod n h+10*div n h?(t++[h])


Try it online!

# 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 the list at the top of the stack
]       # After the loop, wrap all list on the stack into a list
─      # Flatten this list of lists of integers
k     # Push the first integer-input X again
\    # Swap, so the list we created earlier is at the top of the stack
É   # For-each over this list, using three characters as inner code-block:
‼  #  Apply the following two commands separated on the top value:
% #   Modulo
/ #   Integer-divide
]   # After the loop, wrap all values on the stack into a list
x  # Reverse this list
y # Join it together, and convert it to an integer, removing leading 0s
# (after which the entire stack joined together is output implicitly as result)


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

# 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, then append the quotient in unary and the remainder in decimal (ironically Retina gives me the quotient in decimal and the remainder in unary...). (_+,)+(_*)$.2


Convert the final digit to decimal. This also handles the 0 case.

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

# R, 665958 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!

# 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!

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

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

# Charcoal, 20 bytes

Ｆ¬θ0ＷθＦη¿θ«←Ｉ﹪θκ≧÷κθ


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

Ｆ¬θ0


Special-case an input of 0.

ＷθＦη¿θ«


While the input is non-zero, loop over all the bases while the input is non-zero.

←Ｉ﹪θκ


Output the next digit in reverse order.

≧÷κθ


Divide by the base.