8
\$\begingroup\$

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.

Task

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!

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

17 Answers 17

6
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Bah, ninja'd while fixing my answer D: \$\endgroup\$
    – Bubbler
    Commented Apr 30, 2021 at 5:33
4
\$\begingroup\$

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.
\$\endgroup\$
4
  • 2
    \$\begingroup\$ mixed base conversion ftw \$\endgroup\$
    – Razetime
    Commented Apr 30, 2021 at 5:14
  • \$\begingroup\$ You can take the input in reverse, so |.@ is not necessary. \$\endgroup\$
    – Bubbler
    Commented Apr 30, 2021 at 5:26
  • \$\begingroup\$ @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 \$\endgroup\$
    – Jonah
    Commented Apr 30, 2021 at 5:27
  • 1
    \$\begingroup\$ @Jonah Oh, didn't notice that. \$\endgroup\$
    – Bubbler
    Commented Apr 30, 2021 at 5:28
4
\$\begingroup\$

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

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 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)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Welcome back! I hope all is well. \$\endgroup\$
    – Razetime
    Commented Apr 30, 2021 at 11:41
  • 2
    \$\begingroup\$ @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. :) \$\endgroup\$ Commented Apr 30, 2021 at 11:45
3
\$\begingroup\$

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!

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

JavaScript (ES6), 43 bytes

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

Try it online!

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

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.

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

Haskell, 41 bytes

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

Try it online!

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

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

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.

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

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.

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

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

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

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!

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

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!

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

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

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

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

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

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.