# Through the bases

In this challenge, you will be taking a number through all the bases of the rainbow

Given an input, n, you will output n in every base from 9 to 2, and back up to 9. That's 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9.

Example:

>>> 16
17
20
22
24
31
100
121
10000
121
100
31
24
22
20
17


Rules:

• You input can be a string or a number.
• Your output may be separated by new lines, spaces, commas, or tabs.
• You may use built in functions.
• Shortest code wins!
• Can the output be comma-separated? – Downgoat Jul 23 '15 at 23:21
• @vihan1086 yes. – phase Jul 23 '15 at 23:21
• I'm really late on this but can each line be surrounded by quotes? e.g. "17"\n"20" – Piccolo Aug 5 '15 at 5:05

# CJam, 14 bytes

riF,f{7-z2+bN}


Try it online in the CJam interpreter.

### How it works

ri             e# Read an integer I from STDIN.
F,           e# Push [0 ... 14].
f{       } e# For each B in that range, push I and B; then:
7-z2+    e#   Compute abs(B - 7) + 2.
e#   This maps [0 ... 14] to [9 ... 2 ... 9].
b   e#   Perform base conversion.
N  e#   Push a linefeed.

• Just when I thought I had a chance at winning with Pyth. :P – Alex A. Jul 24 '15 at 0:03
• @AlexA. I'm here to save your hope! – orlp Jul 24 '15 at 0:05

# Pyth, 14 bytes

V+_Jr2TtJjkjQN


Needs more work, most code is spent generating 9..2..9.

# Pyth, 1815 14 bytes

Saved 3 bytes thanks to Dennis and 1 thanks to Jakube!

V+r9 1r3TjkjQN


Explanation:

V                     For N in
+r9 1r3T             the list [9,8,...3,2,3,...,8,9],
jQN        convert the input to base N (returns a list),
jk           join the list into a string, implicitly printed.

• "" is just k, p already appends a linefeed and printing is implicit anyway, so FN+r9 1r3TjkjQN is 3 bytes shorter and looks a bit better. – Dennis Jul 23 '15 at 23:54
• @Dennis You're the man. – Alex A. Jul 23 '15 at 23:57

0%_=0
k%b=div k b%b*10+mod k b
g n=map(n%)$[9,8..3]++[2..9]  First, recursively defines a binary operator % to do base conversion, representing the output as a decimal number rather than a string. The function g converts the input n to each base from 9 to 2 and back up. There's probably a shorter approach, advice is welcome. Saved 2 chars on parens thanks to nimi. • Out of curiosity, why does [9,8...3] work but [9..3] doesn't? (the latter seems to return an empty list) – Sp3000 Jul 24 '15 at 0:57 • @Sp3000 It defaults to a step size of +1, so [9..3] is empty because 9>3. Putting the second terms fixes the step size. It general, you can do range(a,b,s) as [a,a+s..b]. – xnor Jul 24 '15 at 1:27 # Julia, 43 bytes n->for i=[9:-1:2,3:9] println(base(i,n))end  This creates an unnamed function that accepts and integer and prints to STDOUT on separate lines. The base function takes two integers: a base (i) and a number (n) and returns the string representation of the number in the given base. Here we just loop through the required bases and print. # Bash + dc, 27 echo {{9..2},{3..9}}o$1p|dc

• Slightly shorter: echo {{9..2},{3..9}}o\$1p|dc – Dennis Jul 24 '15 at 4:45
• @Dennis very good - I didn't know brace expansions could be nested like that! – Digital Trauma Jul 24 '15 at 4:49

## JavaScript (ES6), 57 bytes

f=n=>[...987654323456789].map(v=>n.toString(v)).join


## CoffeeScript, 57 bytes

Different approach:

f=(n)->[-7..7].map((v)->n.toString 2+Math.abs v).join ' '


## JavaScript (ES5), 97 bytes

function f(n){return'987654323456789'.split('').map(function(v){return n.toString(v)}).join(' ')}


### Demo

function f(n) {
return '987654323456789'.split('').map(function(v) {
return n.toString(v)
}).join(' ')
}

document.body.innerHTML = f(16)

## Python 2, 79

g=lambda b,k=input():k and g(b,k/b)*10+k%b
c=7;exec"print g(abs(c)+2);c-=1;"*15


The function g converts a number k to a base b. We can take k as the program input because Python initializes default arguments when the function is defined, not each time it's called. We get the desired sequence of bases by taking the range [-7, -6, ..., 6, 7], taking the absolute value, and adding 2.

# Pip, 14 = 13 + 1 bytes

Uses the -n flag.

aTB2+AB(-7,8)


GitHub repository for Pip

Takes the number as a command-line argument.

        -7,8   Range containing integers from -7 through 7
AB(    )  Absolute value of each
aTB            Convert command-line arg to each base
Print resulting list, newline-separated (-n flag)


It's amazing how all the golfing languages are getting the same score here.

• Those list formatting switches look really useful. – Dennis Aug 5 '15 at 5:25
• @Dennis Yes, the main reason I thought of them was so I wouldn't have to wrap the whole program in (...)Jn (4 bytes vs. 1 with -n) when the program consisted of a map expression (parentheses needed since Map is lower precedence than Join). – DLosc Aug 5 '15 at 5:56

## JavaScript (ES5), 6966 65 bytes

function f(o){for(i=15;i--;)console.log(o.toString(i>7?i-5:9-i))}


# JavaScript SpiderMonkey 31 (1.8), 47 bytes

n=>[for(i of987654323456789)n.toString(i)]+''


It doesn't get simpler than this.

• Nice, but array comprehensions have been dropped from ES7. – Chiru Aug 5 '15 at 5:26
• @Chiru Hm, I'll update it – Downgoat Aug 5 '15 at 5:30

# APL, 52 bytes

I'll admit, this could use more golfing.

{n←⍵⋄⊃{⍺,' ',⍵}/{⊃,/⍕¨(~∧\0=e)/e←(10⍴⍵)⊤n}¨2+|8-⍨⍳15}


it is limited to at most 10 digits output

suggestions for improvement welcome

output for 16 looks like

17 20 22 24 31 100 121 10000 121 100 31 24 22 20 17


## Ruby, 44 bytes

a=->n{(0..14).map{|j|p n.to_s((j-7).abs+2)}}


Call the lambda with a.call(number). This prints out with quotes around each line because p is shorter than puts. I'm assuming this is allowed.

• but n=ARGV[0].to_i;(0..14).map{|j|puts n.to_s((j-7).abs+2)} is 56 bytes and is complete (could be 54 if used p instead of puts – rdnewman Aug 5 '15 at 16:39
• Oh, gotcha, thanks, I'll fix that. Edit: Fixed. Also, defining a function is slightly shorter than getting the parameters passed to the code @rdnewman – Piccolo Aug 5 '15 at 16:53
• But not sure that requiring a.call(number) in IRB would be considered a complete program by others. If it is, then I'd agree with you. Actually, might be able to just do (0..14)map{|j|p ARGV[0]((j-7).abs+2)} and remove the n altogether (didn't test). – rdnewman Aug 5 '15 at 18:31
• @rdnewman I saw a lambda solution like this on an answer with 6 upvotes here so I assumed it was ok. In fact, they didn't even assign it to a variable and just put ->s.. – Piccolo Aug 5 '15 at 18:33
• Ah, ok. Here's the corrected version of the last one I tried: (0..14).map{|j|p ARGV[0].to_i.to_s((j-7).abs+2)}; will run, but 48 bytes to your 44, so if they'll accept it, yours is better. – rdnewman Aug 5 '15 at 18:36