Answers.Body: <p>From <a href="http://en.wikipedia.org/wiki/Chess960_numbering_scheme#Direct_derivation">Wikipedia</a>:</p>
  
  <blockquote>
    <p>White's Chess960 starting array can be derived from its number N (0
    ... 959) as follows:</p>
    
    <p>a) Divide N by 4, yielding quotient N2 and remainder B1. Place a
    Bishop upon the bright square corresponding to B1 (0=b, 1=d, 2=f,
    3=h).</p>
    
    <p>b) Divide N2 by 4 again, yielding quotient N3 and remainder B2. Place
    a second Bishop upon the dark square corresponding to B2 (0=a, 1=c,
    2=e, 3=g).</p>
    
    <p>c) Divide N3 by 6, yielding quotient N4 and remainder Q. Place the
    Queen according to Q, where 0 is the first free square starting from
    a, 1 is the second, etc.</p>
    
    <p>d) N4 will be a single digit, 0 ... 9. Place the Knights according to
    its value by consulting the following table:</p>
  
  <pre><code>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
  </code></pre>
    
    <p>e) There are three blank squares remaining; place a Rook in each of
    the outer two and the King in the middle one.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-07T10:54:43.590Z
Answers.LastActivityDate: 2014-07-07T10:54:43.590Z
Answers.UserDisplayName: WilQu
Body: <p>I am looking into a <a href="http://www.mark-weeks.com/cfaa/chess960/c960strt.htm" rel="noreferrer">website</a> with starting positions for chess960.</p>
  
  <pre><code>000 BBQNNRKR
  001 BQNBNRKR
  002 BQNNRBKR
  003 BQNNRKRB
  004 QBBNNRKR
  005 QNBBNRKR
  ...
  959 RKRNNQBB
  </code></pre>
  
  <p>So there is a table that converts any number from 0 to 959 to a starting position. For example <code>chess960(518) = 'RNBQKBNR'</code> normal starting chess position.</p>
  
  <p>What is this formula? How can I do this without storing all the table?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-02T12:03:07.750Z
Comments.Text: The chess960 sites show you how to do it. Generate them and store them. Then you won't have to calculate them every time.
Comments.UserDisplayName: Tony Ennis
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-02T23:55:00.500Z
Comments.Text: Are you writing a chess program?
Comments.UserDisplayName: NM Wesley Falcao
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-03T01:24:36.673Z
Comments.Text: @wes yes, I am writing a chess server. I want to add ability to play chess960. Currently I have a list of all positions as a list, but having 960 elements is not really nice, especially if there is some formula which can convert a number to this 8 digit string.
Comments.UserDisplayName: Salvador Dali
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-03T03:21:29.313Z
Comments.Text: @SalvadorDali why not nice? If you store them in an array it's less than 8KB. Very performance efficient for a server doing a simple O(1) lookup.
Comments.UserDisplayName: NM Wesley Falcao
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-03T03:27:10.460Z
Comments.Text: I wanted a more elegant approach. O(1) looks good from theoretical computer science approach. I hardly doubt that a formula would be that hard. Even if it would be O(n^2) which is highly unlikely for 960 elements even with the crapiest computer right now will run less than 0.1 second. I was also curious how  this convention appeared, so it is not only about my chess program. Thanks for taking a look at my problem.
Comments.UserDisplayName: Salvador Dali
Tags: <chess-variants><mathematics><chess960>
Title: Is there a formula that converts a number from 0 to 959 to a corresponding chess960 position?
ThreadText: # Is there a formula that converts a number from 0 to 959 to a corresponding chess960 position?
  
  (Asked by: username_0 on 2014-07-02)
  
  I am looking into a website with starting positions for chess960.
  000 BBQNNRKR
  001 BQNBNRKR
  002 BQNNRBKR
  003 BQNNRKRB
  004 QBBNNRKR
  005 QNBBNRKR
  ...
  959 RKRNNQBB
  
  So there is a table that converts any number from 0 to 959 to a starting position. For example chess960(518) = 'RNBQKBNR' normal starting chess position.
  What is this formula? How can I do this without storing all the table?
  
  **Tags:** chess-variants, mathematics, chess960
  
  > **Comments:**
  > **username_1**: The chess960 sites show you how to do it. Generate them and store them. Then you won't have to calculate them every time.  
  > **username_2**: Are you writing a chess program?  
  > **username_0**: @wes yes, I am writing a chess server. I want to add ability to play chess960. Currently I have a list of all positions as a list, but having 960 elements is not really nice, especially if there is some formula which can convert a number to this 8 digit string.  
  > **username_2**: @SalvadorDali why not nice? If you store them in an array it's less than 8KB. Very performance efficient for a server doing a simple O(1) lookup.  
  > **username_0**: I wanted a more elegant approach. O(1) looks good from theoretical computer science approach. I hardly doubt that a formula would be that hard. Even if it would be O(n^2) which is highly unlikely for 960 elements even with the crapiest computer right now will run less than 0.1 second. I was also curious how  this convention appeared, so it is not only about my chess program. Thanks for taking a look at my problem.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2014-07-07. Score: 10
  
  From Wikipedia:
  
  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
    Bishop 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 Bishop 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
    Queen 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 Knights according to
    its value by consulting the following table:
  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 Rook in each of
    the outer two and the King in the middle one.
  
  
  ---
  
  