← Latest papers
💻 computer science

Learning Primality from Modular-Inverse Graphs

This paper demonstrates that GraphSAGE can achieve near-perfect accuracy in distinguishing prime from composite integers by learning structural differences in their modular-inverse graphs, whereas GCN fails to capture these distinctions due to its specific message-passing limitations.

Original authors: Tal Weissblat

Published 2026-09-24
📖 4 min read☕ Coffee break read

Original authors: Tal Weissblat

Original paper licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). ✨ This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Numbers are the building blocks of mathematics, and among them, prime numbers hold a special place. A prime number is a whole number greater than one that can only be divided evenly by one and itself. Numbers that can be divided by other numbers are called composite. For centuries, mathematicians have sought efficient ways to tell these two types of numbers apart, a task that remains vital for modern cryptography and computer security. While traditional methods rely on complex arithmetic calculations, a new line of inquiry asks whether machines can learn to recognize these patterns by looking at numbers not as values, but as shapes. This approach treats the hidden relationships within a number as a map, hoping that the shape of the map reveals the nature of the number itself.

In a recent study, researcher Tal Weissblat explored whether artificial intelligence could learn to distinguish prime numbers from composite ones by examining these mathematical maps. The researcher did not feed the computer the numbers themselves. Instead, every number was transformed into a unique diagram called a modular-inverse graph. To create this diagram, the researcher took a specific number and listed all the smaller whole numbers that could be formed with it. Then, the researcher drew lines between pairs of these smaller numbers if they multiplied together to produce a result that, when divided by the original number, left a remainder of one. This rule was applied exactly the same way to every single number, whether it was prime or composite, without telling the computer which was which. The goal was to see if the resulting shapes naturally looked different depending on the type of number.

The study began with a deep look at the theory behind these shapes. The analysis revealed a clear structural difference between the diagrams of prime numbers and those of composite numbers. For a prime number, the diagram is fully connected in a specific way: every point, except for zero, is linked to at least one other point. There are no lonely points floating alone. In contrast, the diagrams for composite numbers contain isolated points—numbers that have no connections at all. Furthermore, prime numbers produce diagrams with the maximum possible number of connections between distinct points, while composite numbers have fewer connections and those extra lonely points. This theoretical finding suggested that a computer should be able to tell the difference simply by counting connections or spotting the isolated points.

To test this, the researcher trained two different types of artificial intelligence models on a dataset of 10,000 integers, ranging from 2 to 10,001. The data was split so the models learned on smaller numbers and were then tested on larger numbers they had never seen before. One model, known as GraphSAGE, was designed to pay attention to the local neighborhood of each point in the diagram. The other model, a Graph Convolutional Network, used a different method that averages information from neighbors. The results were starkly different. The GraphSAGE model learned the task with remarkable precision, correctly identifying prime and composite numbers in the unseen test set with an accuracy of nearly 99.9 percent. It successfully generalized the patterns it learned from small numbers to much larger ones.

The second model, however, failed completely. It performed no better than random guessing, achieving an accuracy of exactly 50 percent. The theoretical analysis explained why this happened. The GraphSAGE model was able to distinguish between points that had connections and points that stood alone, preserving the crucial structural difference found in the prime number diagrams. The other model, due to the way it averaged information, smoothed out these differences. It treated connected points and isolated points as if they were the same, effectively erasing the very feature that distinguished prime numbers from composite ones. This failure was not a glitch but a fundamental limitation of that specific method when applied to this type of mathematical graph.

The study concluded that the ability to learn primality from these graphs depends entirely on the architecture of the machine learning model. The GraphSAGE architecture proved capable of capturing the subtle structural signatures of prime numbers, while the other common architecture could not. The research also included a check to ensure the model was actually using the graph structure and not just memorizing numbers. When the graph-processing layers were removed, the model's performance dropped back to random guessing. This confirmed that the success came from analyzing the shape of the connections, not from any hidden numerical tricks. The findings demonstrate that arithmetic properties can indeed be encoded into graph structures and learned by machines, provided the machine is built with the right tools to see the differences.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →