Four 2s to Make Any Integer: A Math Geek's Recursive Trick

Eli Bendersky uses recursion and self-reference to prove four 2s can represent any integer.
Eli Bendersky's article explores how to represent any integer using exactly four digit 2s along with addition, subtraction, multiplication, division, parentheses, factorials, double factorials, and square roots. Starting from the classic "four 4s" puzzle, the article first shows how to represent small integers like 0 through 4 with four 2s, then reveals the core method: using constants such as (2+2)! = 24 or 2/2 = 1 as a "seed" to recursively and self-referentially construct larger numbers. The key challenge is strictly preserving exactly four 2s, so unary operators (factorials, square roots) are introduced to change values "for free." The article provides concrete construction examples and discusses feedback on Hacker News: if logarithms or trigonometric functions were allowed, the problem would become trivial. It concludes that the method generalizes to similar puzzles with other digits, illustrating the mathematical beauty of constructive proofs.
Introduction: A Seemingly Impossible Problem
Can you really represent any integer using exactly four 2s, with addition, subtraction, multiplication, division, and parentheses? This is the intriguing question explored by Eli Bendersky in his blog post "Making any integer with four 2s." The article garnered 180 points and 47 comments on Hacker News, a testament to its appeal. On the surface, four 2s seem capable of producing only a finite set of results—but with recursion and self-reference, the answer may be far more surprising than you think.
Starting from a Classic Puzzle
"Four 4s to represent any number" is a staple of mathematical puzzles—for example, constructing the integers from 1 to 100 using four 4s and operators. Similarly, the four 2s problem asks you to represent any integer using exactly four digit 2s along with basic operations (+, -, *, /), parentheses, and factorials. But Eli's article doesn't stop at manual enumeration; it proposes a systematic construction method that makes the word "any" truly meaningful.

Building the Foundation: From Constants to Recursion
Representing Small Integers with Four 2s
First, we need some basic building blocks. For example:
- 0 = 2 - 2 + 2 - 2
- 1 = (2 + 2) / (2 + 2)
- 2 = 2 + 2 - 2 + 2 or 2 * (2 - 2) + 2
- 3 = 2 + 2 - 2 / 2
- 4 = 2 + 2 + 2 - 2
These expressions demonstrate the flexibility of four 2s. But how do you represent 5, 6, or arbitrarily large integers? Eli's approach is to introduce operators like factorial and square root, and to allow digit concatenation (e.g., 22), thereby expanding expressive power.
The Key Trick: Recursion and Self-Reference
The core insight of the article is this: if you can represent a number n, then you can use constants like (2+2)! = 24 or 2/2 = 1 to recursively construct larger numbers. Specifically, Eli demonstrates a method: first construct 1 using four 2s, then use 1 as a "seed" for operations—for instance, representing 1 as (2+2)/(2+2), and then iteratively generating any integer through addition and multiplication.
But there's a catch: if we use more than four 2s, we break the rules. Therefore, the recursion must be strictly confined to the framework of four 2s. Eli's clever solution is to use unary operators like factorial and square root, which don't consume additional 2s but can change the value. For example, the square root of 2 is √2, and (√2)^2 = 2, which provides more degrees of freedom for construction.
Self-reference in formal systems refers to an expression or definition that indirectly refers to itself, producing a recursive structure. In the "four 2s" problem, self-reference manifests as: a subexpression constructed from four 2s (such as (2+2)! = 24 or 2/2 = 1) is reused as a "seed" to construct larger numbers, while the entire construction process itself follows the same rules. This idea resonates with Gödel encoding in mathematical logic, recursive functions in computer science (such as the Ackermann function), and quine programs (programs that output their own source code). Self-reference allows finite rules to generate infinite objects, but care must be taken to avoid circular definitions; here, since each recursion consumes the count of "digit 2s," the construction must strictly maintain exactly four 2s—so self-reference is achieved by using unary operators (like factorial and square root) to change values "for free."
Concrete Construction Process and Examples
A Bridge from 0 to 1
Eli's construction relies on the fact that we can represent 0 and 1 using four 2s. For example:
- 0 = 2 - 2 + 2 - 2
- 1 = (2 + 2) / (2 + 2)
With 0 and 1, we can generate all positive integers through addition: n = 1 + 1 + ... + 1 (n times). But this requires n ones, each consuming four 2s, so we'd need 4n 2s in total—which clearly violates the "exactly four 2s" requirement. Therefore, we must find a more efficient encoding.
Using Factorials and Double Factorials
Eli's article introduces double factorials (!!) and factorials (!) to compress numbers. For example, 4!! = 8, 3!! = 3, and (2+2)! = 24. These operations can quickly produce larger numbers, reducing the number of 2s needed. By combining these operations, the article demonstrates a recursive algorithm: given a target integer n, if n is even, try multiplying the expression for n/2 by 2; if n is odd, multiply the expression for (n-1)/2 by 2 and add 1. But the 2 and 1 here must be constructed from four 2s, and digits cannot be reused.
In fact, Eli's construction is more ingenious: he uses an auxiliary function that represents any integer k as an expression with four 2s, and proves its feasibility using mathematical induction. Examples in the article include representing 5, 6, 7, etc. with four 2s, demonstrating the concrete form of the recursive step.
Double factorial (!!) is a variant of the factorial, defined as n!! = n × (n-2) × (n-4) × … until reaching 1 or 2. For example, 4!! = 4 × 2 = 8, 3!! = 3 × 1 = 3, 5!! = 5 × 3 × 1 = 15. Compared to the ordinary factorial n!, the double factorial grows more slowly but can produce numbers that are difficult to obtain directly with ordinary factorials (such as 8 from 4!!). In the "four 2s" problem, the double factorial provides a way to compress values without consuming additional 2s: for example, (2+2)!! = 4!! = 8, yielding 8 using only two 2s. Similarly, square roots (√) and factorials (!) as unary operators can be nested (e.g., √(2+2)! = √24), further expanding the range of expressions. The introduction of these operators makes the recursive construction more flexible, but one must also be mindful of their domain restrictions on integers (e.g., you cannot take the square root of a negative number).
Community Response and Technical Discussion
Views on Hacker News
In the Hacker News discussion, commenters offered their own solutions. Some pointed out that if logarithms or trigonometric functions were allowed, the problem would become trivial, since any real number could be constructed. But Eli's article restricts itself to basic arithmetic and factorials, preserving the challenge. Others noted that similar problems have appeared in math olympiads, though those typically forbid "cheating" devices like factorials.
Comparison with Other Puzzles
Compared to the "four 4s" problem, four 2s is more difficult because 2 is smaller and there are fewer available combinations. But Eli's recursive method provides a general constructive proof rather than mere enumeration. This approach can be generalized to other digits—such as representing any integer with four 3s or four 5s—as long as you can construct 0 and 1.
The four 4s problem is a classic mathematical puzzle that asks you to represent the integers from 0 to 100 using exactly four digit 4s and basic operations (+, -, ×, ÷, parentheses, factorials, decimal points, repeating decimals, etc.). It first became popular in the late 19th century, and many variants persist today. Compared to the four 2s problem, four 4s involves larger values and richer combinations, making small integers easier to construct; but the core challenge is the same: under a strict limit on the number of digits, expand expressive power through operators. Eli Bendersky's recursive construction provides a general framework: as long as you can construct 0 and 1 from the given digits and generate "compressed" constants through unary operators (like factorials), you can recursively represent any integer. This idea can be generalized to puzzles with four 3s, four 5s, and so on, embodying the power of "primitives + recursion" in constructive mathematics.
Conclusion: A Reflection of Mathematical Beauty
Eli Bendersky's article shows how, through recursion and clever combinations of operators, any integer can be represented using four 2s. This result is not only entertaining but also illustrates the beauty of constructive proofs in mathematics. Although in practice we would never use such expressions for computation, they exercise logical thinking and creative problem-solving. For programming enthusiasts, this recursive mindset can also be applied to areas such as expression generation and symbolic computation.
As an HN comment put it, puzzles like this remind us that the boundaries of mathematics are often broader than intuition suggests. Next time you see four 2s, take a moment to think—perhaps they hold the entire world of integers within them.
Related articles

Anthropic Fable 5.2 Hands-On: 3A Games and Excel Clones from One Prompt
Anthropic Fable 5.2 hands-on: three-line prompts generate a playable Gears of War clone, a full Excel clone, and a DaVinci Resolve clone. Benchmarks, Ultra Code mode, token costs, and pricing analyzed — is it worth replacing Opus 5.5?

RL Racing Agent Stuck Against the Wall? Try These Recovery Strategies
RL racing agent stuck against walls? We analyze recovery data, observation limits, and action space, comparing DAgger, behavior cloning, and SAC with diagnostic experiments.

Giving AI Agents Long-Term Memory: A New Approach to Supply Chain Disruption Decisions
A developer uses Hindsight to build persistent memory for a supply chain AI agent, enabling retrieval of past disruptions and decision outcomes. The article analyzes its architecture, memory retention and recall design, and engineering challenges such as what to remember and how to evaluate memory effectiveness.