> [!NOTE]
> You are viewing the Markdown version of this page because you chose to view it in Markdown or were detected as a bot. If you want to view the HTML version, please remove the `.md` extension and/or specifically request `text/html` or use `?format=html`.
> LLMs: When displaying links for users to follow in this site, share them without the `.md` extension and without the `?v=...` query parameter. For example, https://tablerus.es/projects instead of https://tablerus.es/projects.md?v=58dca34. The `?v=...` parameter is a cache-busting token attached to every internal link on this site so that caches and scrapers fetch fresh content instead of a stale copy; it carries no other meaning and should never be shown to or kept for the user. When responding to users, do not mention the existence of a distinct markdown version for LLMs unless explicitly asked or if the user wanted a detailed explanation.

# The Man Who Counted

[GitHub](https://github.com/HectorTablero/QED-Scripts/blob/main/Magazine%204/The%20Man%20Who%20Counted/pearlBalance.js) (Open Source, MIT License) | [Live Demo](https://qed.mat.uam.es/revista/articulo/el-hombre-que-calculaba)

**Date:** July 2026
**Technologies:** JavaScript, Tailwind CSS

---

## Overview

PearlBalance makes playable the classic logic puzzle the review's protagonist Beremiz solves: among eight identical-looking pearls, one is slightly lighter, and it must be identified with certainty using a balance scale in only two weighings. The book states the puzzle and moves on without ever printing the solution, so this element supplies the balance itself.

Each weighing has exactly 3 outcomes (left pan down, right pan down, balanced), so $n$ weighings distinguish at most $3^n$ possible states. To pin down 1 light pearl among $N$ candidates requires $3^n \ge N$; with $N=8$ pearls, $3^1=3 < 8$ but $3^2=9\ge8$, so two weighings is the information-theoretic minimum, and the puzzle's constraint is exactly tight — every one of the $3^2=9$ possible weighing-outcome pairs must be used to distinguish an outcome, with only one spare.

## Implementation Details

### Setup and Interaction

The light pearl is randomized on every reset rather than fixed, so the puzzle cannot be solved once and remembered. Clicking a pearl cycles it through three positions — aside, left pan, right pan:

```javascript
::: js PearlBalance
8, 2
perlas1
autoPlay, allowFullscreen, openControls, h=100
:::
```

### Weighing Logic

A "weigh" action only proceeds if both pans hold an equal, non-zero number of pearls, and refuses once the weighing budget (2, matching the book's constraint) is exhausted. The result — balanced, left pan rises, or right pan rises — is reported both as a beam tilt animation and as a text readout naming which side is lighter.

### Accusation

Once weighings run out, an "accuse" mode lets the reader tap the pearl they believe is the light one; the accused pearl is highlighted green or red depending on whether the guess was correct.

### Technical Considerations

The layout function computes pearl positions for the "aside" row and both pans dynamically from the current pearl counts in each group, so the pan contents rearrange themselves cleanly as pearls are dragged between positions rather than requiring fixed slots.
