Autogram Algorithms
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
An autogram is a self-descriptive sentence: the text talks about its own composition and still remains true. That makes autogram generation an algorithmic fixed-point problem, where the counts described by the sentence must match the counts produced by the sentence itself.
What Makes Autograms Hard
A normal counting problem is one-way: you inspect a finished string and count letters. An autogram is circular. If the sentence says it contains five a characters, writing the word five may change the letter counts and invalidate the claim.
That feedback loop is why autogram algorithms usually look like search procedures rather than direct formulas. You start with a template, estimate the counts, rebuild the sentence with those counts, and repeat until the description stops changing.
A full English autogram can track many letters, digits, punctuation marks, or even words. The more symbols you track, the harder convergence becomes because each update changes several counts at once.
Modeling the Problem as a Fixed Point
A practical way to think about an autogram algorithm is this:
- choose a sentence template with blanks
- fill the blanks with candidate counts
- count the resulting text
- compare the counted values to the claimed values
- keep searching until both match
For some templates, brute force is acceptable. For larger search spaces, people use heuristics, local search, or constraint solvers.
The Python example below solves a toy version. It searches for a sentence that correctly states how many a and b characters it contains:
This example is intentionally small, but the structure is the same as larger autogram generators: represent, count, compare, search.
Search Strategies
Brute force works only when the candidate space is tiny. Once you move to ten or twenty tracked symbols, naive enumeration becomes impractical.
A common improvement is iterative repair. Start with guessed counts, generate the text, recount the symbols, and use the new counts as the next guess. Sometimes this converges quickly; sometimes it oscillates or lands in an impossible region of the search space.
Another approach is constraint solving. If you can express the text layout and counting rules symbolically, a solver can prune huge parts of the search space. That is often more effective than repeatedly rebuilding long sentences by hand-coded heuristics.
There is also a language-design component. Good autogram templates reduce ambiguity and keep the search space manageable. Short, repetitive number words often help, while elaborate phrasing makes the count interactions much harder to stabilize.
Why This Topic Matters
Autograms are mostly recreational mathematics and computational linguistics, but the algorithmic lesson is broader. They are compact examples of self-reference, fixed points, and constraint satisfaction. Those ideas show up in program analysis, theorem proving, compiler design, and formal verification.
They are also useful teaching tools. A student who understands why an autogram is difficult usually understands why feedback loops in larger systems can be difficult as well. The sentence changes the counts, and the counts change the sentence.
Common Pitfalls
The first pitfall is ignoring the counting rules. Decide early whether spaces, punctuation, uppercase letters, and digits count. Different rules produce different answers.
The second is using a template that makes a solution impossible. If the number words inject too many copies of the letters you are tracking, the search can never succeed. That is not a bug in the code; it is a bad problem formulation.
The third is assuming iterative repair will always converge. Some templates bounce between two or more near-solutions forever. In those cases you need cycle detection, backtracking, or a different template.
Summary
- Autograms are self-descriptive texts whose claims must match their actual contents.
- Generating one is a fixed-point search problem, not a simple counting task.
- Small templates can be solved by brute force; larger ones need heuristics or constraints.
- The template design matters as much as the search algorithm.
- Autogram algorithms are useful examples of self-reference and convergence in computation.

