I am not looking for an efficient way to find primes (which of course is a solved problem). This is more of a "what if" question.

So, in theory, could you train a neural network to predict whether or not a given number $n$ is composite or prime? How would such a network be laid out?

  • 3
    $\begingroup$ If the primes follow a pattern and someone just happens to train a neural network with enough hidden nodes in order to define the classification boundary, I suppose it would work. However, we don't know if that classification exists and even if it did, we would have to prove what the boundary is in order to prove that the neural network did indeed find the correct pattern. $\endgroup$
    – quintumnia
    Commented Jun 5, 2017 at 7:31

5 Answers 5


Early success on prime number testing via artificial networks is presented in A Compositional Neural-network Solution to Prime-number Testing, László Egri, Thomas R. Shultz, 2006.

The knowledge-based cascade-correlation (KBCC) network approach showed the most promise, although the practicality of this approach is eclipsed by other prime detection algorithms that usually begin by checking the least significant bit, immediately reducing the search by half, and then searching based other theorems and heuristics up to $floor(\sqrt{x})$. However the work was continued with Knowledge Based Learning with KBCC, Shultz et. al. 2006

There are actually multiple sub-questions in this question. First, let's write a more formal version of the question: "Can an artificial network of some type converge during training to a behavior that will accurately test whether the input ranging from $0$ to $2^n-1$, where $n$ is the number of bits in the integer representation, represents a prime number?"

  1. Can it by simply memorizing the primes over the range of integers?
  2. Can it by learning to factor and apply the definition of a prime?
  3. Can it by learning a known algorithm?
  4. Can it by developing a novel algorithm of its own during training?

The direct answer is yes, and it has already been done according to 1. above, but it was done by over-fitting, not learning a prime number detection method. We know the human brain contains a neural network that can accomplish 2., 3., and 4., so if artificial networks are developed to the degree most think they can be, then the answer is yes for those. There exists no counter-proof to exclude any of them from the range of possibilities as of this answer's writing.

It is not surprising that work has been done to train artificial networks on prime number testing because of the importance of primes in discrete mathematics, its application to cryptography, and, more specifically, to cryptanalysis. We can identify the importance of digital network detection of prime numbers in the research and development of intelligent digital security in works like A First Study of the Neural Network Approach in the RSA Cryptosystem, G.c. Meletius et. al., 2002. The tie of cryptography to the security of our respective nations is also the reason why not all of the current research in this area will be public. Those of us that may have the clearance and exposure can only speak of what is not classified.

On the civilian end, ongoing work in what is called novelty detection is an important direction of research. Those like Markos Markou and Sameer Singh are approaching novelty detection from the signal processing side, and it is obvious to those that understand that artificial networks are essentially digital signal processors that have multi-point self tuning capabilities can see how their work applies directly to this question. Markou and Singh write, "There are a multitude of applications where novelty detection is extremely important including signal processing, computer vision, pattern recognition, data mining, and robotics."

On the cognitive mathematics side, the development of a mathematics of surprise, such as Learning with Surprise: Theory and Applications (thesis), Mohammadjavad Faraji, 2016 may further what Ergi and Shultz began.


I'm an undergraduate researcher at Prairie View A&M university. I just spent a few weeks tweaking an MLPRegressor model to predict the $n$th prime number. It recently stumbled into a super low minimum, where the first $1000$ extrapolations outside of the training data produced error less than $.02$ percent. Even at $300000$ primes out, it was about $.5$ percent off. My model was simple: $10$ hidden layers, trained on a single processor for less than 2 hours.

To me, it begs the question, "Is there a reasonable function that produces the nth prime number?" Right now, the algorithms become computationally very taxing for extreme $n$. Check out the time gaps between the most recent largest primes discovered. Some of them are years apart. I know it's been proven that if such a function exists, it will not be polynomial.

  • 1
    $\begingroup$ Hi Cody, this wasn't long ago. But I would like to have a chat with you regarding the test you did. Would you be willing to live chat about what you did and what you perceived? I would like to see if there is a possibility to experiment further with this. $\endgroup$
    – mjs
    Commented Jul 22, 2019 at 18:43
  • $\begingroup$ Did you write this up? I'd love to read a summary if you have it. $\endgroup$
    – cjm2671
    Commented Aug 24, 2020 at 16:28
  • $\begingroup$ I suggest that you replicate the experiment on something else, for example, the divisible of 3 or other more complicated math predictions. Let see if something that we know has an equation can be predicted $\endgroup$
    – Oren
    Commented Feb 22, 2021 at 22:04

In theory, a neural network can approximate any given function. This result is known as the universal approximation theorem.

However, if you train a network with the numbers $0$ to $N$, you cannot guarantee that the network will classify numbers outside that range correctly ($n > N$).

Such a network would be a regular feed-forward network (or MLP) as recurrency does not add anything to the classification of the given input. The number of layers and nodes of that NN can only be found through trial and error.

  • 5
    $\begingroup$ Universal theorems applies to continuous functions on compact subsets. Prime/not prime is not such kind of function. $\endgroup$ Commented Aug 17, 2018 at 10:59
  • 3
    $\begingroup$ @pasabaporaqui: In this case the primeness function can be approximated well enough by a continuous function with peaks at the values of primes. So the NN might output 90% chance of being a prime for 6.93 - that is clearly nonsense, but if you discretised the inputs and outputs, you don't really care about what the NN would predict for non-integers. I think this answer is basically correct. $\endgroup$ Commented Aug 27, 2018 at 9:00

From a purely theoretic standpoint, yes. Such a neural network(NN) can be manually constructed.

A neuron is very similar to a logic gate. And it is definitely possible to use them as logic gate, replicate the structure of a CPU, and then perform any algorithm we already have. So in theory, YES. (Albeit obviously dumb to do so)

The more complicated question is whether NN can "learn" to detect primes. Given that it is theoretically possible, and also that the human brain is literally NN, I am very inclined to guess that it is possible. But not with a feedforward network.

Prime detection is not a very NN friendly task for two reasons:

  1. It is discrete maths, and NN are more fuzzy in nature.
  2. All currently known algorithm of prime detection requires some sort of loop / iteration, making them complexity >= O(Bits). Where as feed forward NN is constant time complexity.

The second difficulty will largely determine the shape of the final NN. Assuming there is no constant time detection algorithm of prime ever possible, the NN will not be feed forward. A recurrent network is required such that the NN can deal with loops.

The first difficulty means that the NN will likely be rather complex and a lot of examples will be needed to overcome the inherent fuzziness. But that I guess is very much expected.


yes it is feasible, but consider that integer factorization problem is an NP-something problem and BQP problem.

because of this, it is impossible that a neural network purely based on classical computing finds prime number with 100% accuracy, unless P=NP.

  • 1
    $\begingroup$ As the question explains, check if a number is prime is not a NP problem. $\endgroup$ Commented Aug 17, 2018 at 10:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .