This project explores a fundamental question:
Can neural networks actually learn the structure of prime numbers?
Prime numbers are not defined by simple patterns — they follow strict number-theoretic rules based on divisibility.
This project began as an MSc Semester 1 experiment and was later extended to investigate:
- Whether neural networks can learn primality from data
- Whether different architectures improve performance
- Whether models generalize to unseen numerical ranges
Each integer is represented using:
-
Binary representation
- Fixed-length encoding (~21 bits)
-
Modular features
- Remainders with small primes (mod 2, 3, 5, ..., 31)
-
MLP (Multi-Layer Perceptron)
- Binary only
- Binary + modular
- Modular only
-
CNN (1D)
-
Transformer
- High recall but very low precision
- Large number of false positives
- Unable to capture divisibility structure
👉 Neural networks fail to infer primality from binary representation alone.
- No meaningful gain over simple MLP
- Still fail to learn arithmetic relationships
👉 Increasing architectural complexity does not address the core issue.
The improvement obtained using modular features does not indicate that the model has learned the concept of primality.
Instead, the input representation already contains direct information about divisibility. Since primality is fundamentally defined in terms of divisibility by smaller integers, providing modular residues effectively injects the underlying mathematical structure into the input.
In this setting, the model is not discovering the rule governing prime numbers. It is simply learning how to combine already relevant signals that encode divisibility properties.
Therefore, the apparent performance gain reflects the quality of the engineered features rather than the model’s ability to infer number-theoretic structure.
Neural networks do not naturally learn number-theoretic structure from raw representations.
There is a fundamental mismatch:
- Input representation → positional (binary encoding)
- True structure → divisibility (modular arithmetic)
For large integers:
- Primality testing is computationally non-trivial
- Providing modular features already requires partial computation
👉 Therefore, feature engineering is not a true solution — it bypasses the original problem.
- Binary models → high recall, very low precision
- CNN / Transformer → similar failure behavior
- Modular features → strong metrics but not genuine learning
👉 Detailed plots and comparisons are available in the results/ folder
prime-number-ml/
│── notebooks/ # Experiments and model runs
│── src/ # Dataset and model definitions
│── results/ # Evaluation plots and comparisons
│── data/ # Dataset instructions (not included)
Dataset is not included due to size.
To generate the dataset:
notebooks/dataset_generation.ipynb
- Train range: 2 → 750,000
- Test range: 750,001 → 1,250,000
This project suggests that:
- Neural networks struggle with discrete mathematical rules like primality
- Learning arithmetic structure is fundamentally different from pattern recognition
- Additional model complexity does not solve this limitation
- Hybrid symbolic + neural approaches
- Explicit modeling of divisibility
- Alternative representations of integers
Soumodip Dey
⭐ If you found this interesting, consider starring the repository.