Testing Transformer Learnability on the Arithmetic Sequence of Rooted Trees

Alessandro Breccia, Federica Gerace, Marco Lippi, Gabriele Sicuro, Pierluigi ContucciarXiv 2025 · DOI

Abstract

This paper investigates the ability of Transformer models, specifically GPT-2, to learn the underlying structural "grammar" of arithmetic. By representing the prime factorization of the first 100 billion integers as rooted planar trees and encoding them into symbolic sequences, the authors create a novel "arithmetic text" benchmark. Results indicate that while Transformers significantly outperform Markov baselines in capturing local multiplicative structures, they face fundamental limitations in predicting global properties like prime distribution due to context window constraints.

Introduction

Prime factorization is a cornerstone of number theory and cryptography, yet it remains computationally difficult as numbers scale. Traditional machine learning approaches often struggle with numerical data because standard encodings (binary or decimal) appear as noise to neural networks. This study explores an alternative: treating arithmetic as a language. By converting the iterated prime factorization of integers into a symbolic sequence called $\mathcal{N}\mathcal{T}$, the researchers test whether a standard Transformer can learn the "grammar" of numbers to predict unseen factorizations.

Methodology: From Numbers to Trees

The core of this approach is the mapping of every integer $n$ to a rooted tree. In this representation, the root's children represent the prime factors of $n$. Crucially, the exponents of those factors are then recursively factorized into their own sub-trees until the process reaches 1.

Recursive mapping of an integer $n$ to a rooted tree based on its prime factorization.

These trees are subsequently converted into "Dyck words" (balanced strings of 1s and 0s). For example, a prime number is always represented by the string 10. These binary strings are then compressed using Byte-Pair Encoding (BPE) into a vocabulary of tokens, creating a deterministic "arithmetic text."

The Arithmetic Text Dataset

The study utilizes a massive dataset consisting of the first $10^{11}$ (100 billion) natural numbers. This sequence, $\mathcal{N}\mathcal{T}$, exhibits long-range correlations and sublinear dictionary growth, mirroring the structural complexity of natural language.

The sequence of rooted trees representing the first 16 natural numbers, forming the basis of the arithmetic text.

The model, a GPT-2 architecture with 87 million parameters, was trained on two self-supervised tasks: Next-Word Prediction (NWP) and Masked Language Modeling (MLM). Performance was benchmarked against a Markov Chain (MC) to determine if the Transformer was capturing deep hierarchical rules rather than simple local transitions.

Key Insights and Results

The Learning Curve

The model's loss curves revealed a distinct two-phase learning process. An initial rapid decrease was followed by a plateau, after which a second descent occurred. This suggests the model first masters simple statistical frequencies before discovering more complex, recursive grammatical rules inherent in prime factorization.

Local vs. Global Reasoning

While the GPT-2 model significantly outperformed the Markov baseline, it encountered a "representational boundary" at prime numbers. The model excelled at capturing local multiplicative correlations—such as identifying square-free numbers—but struggled with the global logic required to predict primes perfectly. Because the appearance of a prime is not strictly determined by the numbers immediately preceding it, the model's limited context window (1024 tokens) prevents it from mastering the distribution of primes.

Quantitative Performance

  • Word Accuracy: In low-temperature regimes, token accuracy remained above 0.4, even with 50% masking.
  • Prime Prediction: For the specific word representing primes (10), the model achieved precision and recall of approximately 0.3. While not perfect, this is significantly better than random chance for numbers of this magnitude.
  • F1 Scores: For certain square-free integers, F1 scores reached 0.4 to 0.5, demonstrating a robust grasp of specific arithmetic structures.

Conclusion and Future Work

The study demonstrates that Transformers can partially learn the internal syntax of arithmetic. However, the distribution of primes remains a challenge due to its global nature. Future research may involve scaling to larger models with wider context windows to see if deeper hierarchical relations can be reconstructed. Additionally, probing the model's latent space could reveal whether its internal vector representations mirror algebraic proximity between integers.

Summary updated

BibTeX
@misc{breccia2025testing,
  author = {Alessandro Breccia and Federica Gerace and Marco Lippi and Gabriele Sicuro and Pierluigi Contucci},
  title = {{Testing Transformer Learnability on the Arithmetic Sequence of Rooted Trees}},
  year = {2025},
  eprint = {2512.01870},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2512.01870},
}
Figure