LockerGnomeTech news and nostalgia for today's technology professionals
Subscribe

Search

Software

He assigned letters prime numbers so every Scrabble word’s anagram key fits in 64 bits

David Andersen’s word-game detour shows how dictionary-specific guarantees can shrink the key before anyone reaches for exotic hardware.

Chris in terrazzo, kneeling to slide blank letter tiles into a row while a number counter glows beside him
Illustration: LockerGnome

PSYCHOPATHOLOGY nearly breaks the calculator. Its prime-product key lands near the ceiling of an unsigned 64-bit integer, but stays underneath it. Finally, a word that sounds expensive has a number to match.

David Andersen’s write-up starts with a word-ladder game whose allowed moves include rearranging letters. Searching all the anagrams meant building an index, and the obvious sorted-string approach gave him a baseline worth picking apart. This is his experiment on his dictionary, with his timings.

Multiplication provides the neat trick. Assign each letter a different prime, then multiply the values for every letter in a word. Rearranging those letters leaves the product unchanged. Repeating a letter repeats its factor, so the counts survive too. The math does the sorting without arranging a single tile.

A fixed-width integer complicates that elegance. A product that overflows can wrap around, losing the uniqueness the mathematics promised. Alphabetical prime assignment overflows on a word in his dictionary. The container has a lid (how inconsiderate).

He instead tunes the assignment against long words in the actual corpus. Letters that create troublesome products get smaller primes. This preserves the prime-factor guarantee for the tested dictionary while fitting the result into the chosen integer. It does not promise that an arbitrary future word list will also fit.

The largest product belongs to PSYCHOPATHOLOGY. Andersen describes the result as a “collision-free canonical 64-bit identifier.” The qualification lives in the corpus: a Scrabble word list, not every string anyone might type tomorrow.

The timing progression also resists a convenient headline shortcut. Prime products alone do not produce the final 1.96 ms. His sequence also changes allocation behavior, packed representations, the hash table and the multiplication loop. Giving the prime trick all the credit would leave several other optimizations standing awkwardly outside the photo.

For someone evaluating a similar optimization, the useful question is whether the input set has a stable boundary. A known dictionary can be checked completely. User-submitted names, arbitrary identifiers or an expanding language collection demand a fresh overflow argument. A smaller key becomes a liability when its quiet assumption changes underneath it.

A sensible reproduction would keep the corpus constant, compare the groups against the simple implementation, and check every product for overflow. Then each timing step could be measured independently. Otherwise a faster answer might merely be a different answer wearing running shoes.

The larger possibility is specialized indexing built around real input constraints. That can remove work before a faster machine ever enters the discussion. Andersen’s next promised installment is the complete word-ladder solver; the index has finished its sprint while the original game still waits.

PSYCHOPATHOLOGY still fits.

More Software

Every Software story →