Chess960 position lookup This is a follow-up to Chess960 position generator.

In Chess960, there are 960 possible starting positions that can be enumerated from 0 to 959 (or, at your choice, from 1 to 960). The enumeration scheme is defined in http://en.wikipedia.org/wiki/Chess960_numbering_scheme:


  White's Chess960 starting array can be derived from its number N (0
  ... 959) as follows:
  
  a) Divide N by 4, yielding quotient N2 and remainder B1. Place a
  B​ishop upon the bright square corresponding to B1 (0=b, 1=d, 2=f,
  3=h).
  
  b) Divide N2 by 4 again, yielding quotient N3 and remainder B2. Place
  a second B​ishop upon the dark square corresponding to B2 (0=a, 1=c,
  2=e, 3=g).
  
  c) Divide N3 by 6, yielding quotient N4 and remainder Q. Place the
  Q​ueen according to Q, where 0 is the first free square starting from
  a, 1 is the second, etc.
  
  d) N4 will be a single digit, 0 ... 9. Place the K​n​ights according to
  its value by consulting the following table:

Digit     Knight positioning
0         N   N   -   -   -
1         N   -   N   -   -
2         N   -   -   N   -
3         N   -   -   -   N
4         -   N   N   -   -
5         -   N   -   N   -
6         -   N   -   -   N
7         -   -   N   N   -
8         -   -   N   -   N
9         -   -   -   N   N

  
  e) There are three blank squares remaining; place a R​ook in each of
  the outer two and the K​ing in the middle one.


You can find the complete table at http://www.mark-weeks.com/cfaa/chess960/c960strt.htm.

Your task: Write a program that takes an integer as its input and returns the white baseline pieces for that index, e.g.

f(1) = "BQNBNRKR"
f(518) = "RNBQKBNR"


Or, you might as well use unicode "♖♘♗♕♔♗♘♖" if your language supports that.

For your inspiration, here's a pretty straight-forward (not optimized) JS implementation: http://jsfiddle.net/Enary/1/
Python, 179 chars



i=input()
a=i%4*2+1
b=i/4%4*2
c=i/16%6
d=i/96*5
s='NNRKRNRNKRNRKNRNRKRNRNNKRRNKNRRNKRNRKNNRRKNRNRKRNN'[d:d+5]
s=s[:c]+'Q'+s[c:]
if a&gt;b:a,b=b,a
print s[:a]+'B'+s[a:b-1]+'B'+s[b-1:]


Builds it backwards from the description order so we don't have to count the spaces that are already taken.