Decorative fleuron

The Library as a Function: A Mathematical Representation of Borges’ Library of Babel

In his short story The Library of Babel, Jorge Luis Borges describes a library “composed of an indefinite, perhaps infinite number of hexagonal galleries”. Within these galleries, 4 of the walls are of bookshelves, each holding 5 shelves of 32 books. Each book has 410 pages, with 40 lines to a page and 80 characters to a line.

Each book is composed of the same 25 symbols:

This library is said to be “perfect, complete, and whole”. That is to say that it contains every possible string over this alphabet across the size of each book. We are also told that “there are no two identical books”, meaning that the number of books in the library is exactly equal to the number of possible strings of length 1,312,000 (410 × 40 × 80) over a 25-symbol alphabet.

The concept of the library is a thought-provoking one, and the narrative that Borges builds around it is wonderful. If you have not already, I encourage you to read the story for yourself before you continue with this post.

Although Borges describes it as “perhaps infinite”, the fixed alphabet and fixed book length, along with the stipulation that no two books are identical, imply that only finitely many books can exist.

If the library is finite, then, is it possible to construct it? Perhaps not from bricks and mortar, but what about an abstract representation of it? In this post we will define a complete coordinate system that represents the library, and allows us to access it as if it physically existed.

I put this system to use on my website libraryofbabel.app, which implements a browsable, searchable, true-to-scale Library of Babel in your web browser.


Going forward, a book is a string of length L=1,312,000L = 1,312,000 over our 25-symbol alphabet. It follows that the total number of possible books is

N=251312000N = 25^{1312000}

To give some idea of scale, this number has about 1.8 million decimal digits!

Since we know each book is unique, we can consider that every possible book corresponds to a unique element of the set

S={0,1,2,...,N1}S = \{0,1,2,...,N-1\}

We will use elements of S in two roles: an index number xx indicating a book’s position in the library, and a content number yy representing the base-25 encoding of that book’s text.

Since we are working with integers, and we wish to somehow represent the contents of the library’s books using these numbers, we need a way to encode and decode between the integer representation of a book’s contents and the alphabetical representation of the same contents. To do this, we simply convert integers to base-25 using our 25 character alphabet as the character set.

Thus the contents of each book, when interpreted as base-25, form a content number ySy \in S.

In order to represent the library, we must find a mathematical correspondence that links each integer in this set to exactly one of these books. This would give us a library where the same book always exists at the same position.

This is all well and good, but the most profound thing about the Library of Babel is the fact that anything that has ever been written, and indeed ever will be written, exists within it. For our representation of the library to be at all interesting, we should be able to look up the contents of a book, and know where it resides within the library.

In summary, we want:

  1. A way to map each index number xx to a unique content number yy.
  2. A way to recover xx from yy.

With a method satisfying both of these requirements, we will have arrived at an accessible library: given the index of a book we can look up its contents, and given the contents of a book we can look up its position in the library.


The mathematical basis for our function is that for any integer mm, multiplication by a number CC that is coprime to (shares no common factors with) mm induces a permutation of {0,...,m1}\{0,...,m-1\}.

In our case, m=N=251312000m = N = 25^{1312000}, and therefore CC must not be divisible by 5.

If we consider the mapping

xy=(Cx)modNx \mapsto y = (Cx) \bmod N

then each index xx is assigned a distinct content number yy, which can be converted into base-25 to obtain a book of length 1,312,000 (once we pad the output with as many leading zeroes as required).

Moreover, because multiplication by a coprime number in modular arithmetic has an inverse, each index corresponds to exactly one content number, and vice versa. More formally, there exists an integer II satisfying

(CI)modN=1(CI) \bmod N = 1

This integer II is the modular inverse of CC modulo NN. Once II is chosen, it follows that

yx=(Iy)modNy \mapsto x = (Iy) \bmod N

recovers the unique index for that content. The map is therefore bijective: no book is omitted, no book is produced twice, and everything can be reversed.

Conceptually, we may think of the entire set of integers modulo NN as a ring under addition and multiplication. If CC belongs to the subset of elements that have multiplicative inverses (in abstract algebraic terms, the set of units in the ring), then the mapping

xCxx \mapsto Cx

permutes that entire ring. This theoretical foundation guarantees both uniqueness of labels and reversibility of the assignment.

Given a large and random-looking CC, we get neighbouring outputs that appear unrelated (though not cryptographically random). Another application of this concept can be found in the blog post ‘a practical use of multiplicative inverses’.

But why bother with this permutation at all? Could we not just say that the book’s index is the book’s contents?

This is mostly a stylistic choice. Borges never specifies how books are arranged, and the text gives no rule governing relationships between nearby volumes.

This method too would give us a complete library, whereby every possible book exists, and contains no two identical books. Representing the library this way gives us sequential books, i.e. there is at least some similarity between one book and its immediate neighbour. Personally, I prefer the ‘random’ nature this permutation provides. It is worth noting that the permutation does not change the set of books, it only reorders them.

It is also worth noting that any permutation of the set SS would work. Multiplication by a unit modulo NN is simply one of the easiest to compute, and satisfies nicely the stylistic requirement of random-looking neighbours.


But why does this work? Consider the map

x(Cx)modNx \mapsto (Cx) \bmod N

If x1x_1 and x2x_2 are different integers in the range SS, then (Cx1)modN(Cx_1) \bmod N will not coincide with (Cx2)modN(Cx_2) \bmod N. No collisions occur because having

(Cx1)(Cx2)(modN)(Cx_1) \equiv (Cx_2) (\bmod N)

would imply NN divides C(x1x2)C(x_1 - x_2). But if CC and NN share no prime factors, the only possibility is that NN must divide (x1x2)(x_1 - x_2). Yet x1x_1 and x2x_2 both lie in the interval SS, so

x1x2<N\left\lvert{x_1-x_2}\right\rvert \lt N

Hence x1x2=0x_1 - x_2 = 0, meaning x1=x2x_1 = x_2, contradicting the assumption that they were distinct; thus the mapping is injective. At the same time, since it is injective in a finite set of size NN, it is also surjective, and thus every element of SS is hit exactly once when we take all possible xx in that same set.


And so we arrive at a scheme that realises both the spirit and structure of Borges’s Library in a well-defined manner, and the goals outlined at the outset are met. We have defined a deterministic coordinate system for Borges’ library; a bijective indexing function over the space of all books.

By viewing each book as a string of 1,312,000 characters in a 25-symbol alphabet, it becomes straightforward to represent every possible book as a base-25 integer. The key lies in selecting an arithmetic operation, multiplication by a chosen constant modulo N=251312000N = 25^{1312000}, that permutes these integers without any overlap.

This ensures that each index xx in the library corresponds to exactly one book (content number yy), and the existence of a multiplicative inverse for that constant permits a direct path from any given book’s contents to the integer that generated it.

Although the library’s size surpasses normal comprehension, it remains a finite—albeit immense—entity whose every book can, in principle, be located, examined, and then relocated precisely by virtue of this modular mapping.