How to get the ordinal number of a book on a bookshelf?

76 Views Asked by At

I'm doing a programming project similar to the Library of Babel and I need to find out the ordinal number of a book in a library.

  • The library has $10$ rooms ($a$)
  • Each room has $4$ walls ($b$)
  • Each wall has $5$ shelves ($c$)
  • Each shelve has $32$ books ($d$)

Now my question is: if I know what room ($a$), wall ($b$), shelve ($c$) and book on that shelf ($d$), how would I find the ordinal number of that book in the library ($n$)?

Also I want to know how to do it the other way around. If I know the ordinal number ($n$), how would I find ($a$), ($b$),($c$) and ($d$)?

It might seem like multiplying them together would work, but because of commutative properties of multiplying ($ab = ba$) I get some conflicts if we simply swap out (for ex.) the book ordinal number for the room number.

Any help is appreciated.

Edit for clarification: The first book is in room 1, wall 1, self 1 and book number 1. The ordinal for that book is 1.

1

There are 1 best solutions below

2
On BEST ANSWER

Let's assume that all numbers start with $0$. That is, $a\in\{0,\ldots,9\}$, $b\in\{0,\ldots,3\}$, $c\in\{0,\ldots,4\}$ and $d\in\{0,\ldots,31\}$. Then one way to get an ordinal number is: $$n=32(5(4a+b)+c)+d$$ The reverse direction is then:

  • Divide $n$ by 32. The remainder is $d$.

  • Divide the quotient from the previous step by $5$. The remainder is $c$.

  • Divide the quotient from the previous step by $4$. The remainder is $b$.

  • The quotient from the previous step is $a$.

To get the equivalent numbering for numbers starting at $1$, just subtract $1$ from each number before doing the calculation and at the end add $1$ to the result.