Approximation model based on LSTM for predicting the next prime number in an infinite sequence

. Prime numbers are a special set of natural numbers that have captured the attention of mathematicians since ancient times. As prime numbers are a fundamental component in many areas of mathematics, they have naturally found wide applications in various fields of knowledge, such as cryptography. The goal of all researchers is to discover the distributional relationships within this infinite set of numbers or, at the very least, to create a mathematical model for predicting the next prime number in a diverging sequence. This article is dedicated to an attempt at solving this problem based on a deep learning model - Long Short-Term Memory (LSTM) neural network.


Introduction
Natural numbers that are different from one and have only two divisors -the number itself and one -are commonly referred to as prime numbers. The study of prime numbers dates back to ancient times, with the first mentions of them appearing as early as the Upper Paleolithic era [1].
However, despite the fact that scientists have been studying prime numbers for over five thousand years, it is important to note that a formula relating the numerical sequence and allowing the generation of the next prime number in the series has not yet been obtained.
Given the fact that the sequence of prime numbers is infinite, the task of determining the next prime number in the sequence remains relevant and can be approached through two distinct methods: 1. Deterministic primality tests: These tests aim to precisely determine whether a given number is prime or composite. By establishing the membership of a number in one of the two categories, these tests provide an exact result. 2. Probabilistic primality tests: These tests estimate the probability of a number belonging to the prime number category and therefore cannot guarantee complete reliability.
While the first category of tests allows for a definitive classification of numbers, it requires significant time and computational resources to execute the testing procedure. The second category reduces the time and computational requirements by providing a predictive result at the cost of some reliability. To confirm or refute the prediction, an actual primality test must be performed in either case.
The significant time investments required to compute the next prime number in an infinite sequence necessitate the development of a prediction algorithm that combines the speed of probabilistic primality tests with a level of reliability approaching that of deterministic primality tests. As one variation of such an approximation model, the authors of this article have devised a deep learning model based on the logic of Long Short-Term Memory (LSTM) neural networks.

Materials and Methods
The choice of Long Short-Term Memory (LSTM) models is not accidental but justified by a specific characteristic of the prime number sequence. To formalize this characteristic, a mathematical operation of taking the difference between each pair of adjacent elements in the numerical sequence should be performed. In other words, we obtain a sequence of differences between prime numbers that exhibits certain properties explicitly derived from the properties of the prime number sequence: 1. The sequence of differences will be non-decreasing and composed of natural numbers; 2. The sequence of differences is unbounded from above and bounded from below; 3. The series of numbers will be infinite, indicating that the sequence itself is divergent.
Having defined the properties of the data, we can now proceed to examine their peculiarities. Figure 1 illustrates a fragment of the prime number sequence.  By comparing the figures, it can be observed that the second sequence contains repeated elements. Furthermore, all of these repetitions are even numbers (except for the first difference), and the frequency of occurrence of the difference repetitions can be calculated. The ranking of the differences was performed based on the increasing magnitude of the difference and computed for prime number values limited to the first million values ( Figure  3). The comparison of differences reveals an important characteristic in the data: values that are multiples of 6 exhibit a higher frequency of repetitions.
To confirm this observation, let us consider the numerical sequence of differences between prime numbers based on the restriction of prime number series up to ten million values (Figure 4). If there is a change in the distribution trend, it will be reflected in the diagram itself. Fig. 4. The distribution of frequencies of repeated differences for prime numbers, limited to values exceeding ten million As evident from the provided diagram (Figure 4), when restricting the sequence of prime numbers to the first ten million values, a trend of the highest popularity of differences divisible by 6 is maintained.
A similar distribution pattern is observed for differences between prime numbers up to one hundred million values ( Figure 5). This statement holds true for every "triplet" of difference values, thus playing a key role in deep learning models with a memory effect, with the long short-term memory (LSTM) neural network being a prominent example [2].

Results and discussion
The task of predicting the next prime number in an infinite sequence can be reformulated as the problem of determining the next difference between the positions of adjacent prime numbers. By formulating the task in this way, the main idea is preserved: knowing the current prime number allows us to add the predicted difference to it, thus obtaining the next prime number.
To implement a software solution, it is necessary to use a long short-term memory (LSTM) neural network. This is justified by the fact that the most frequently occurring differences, which are multiples of 6, can be more accurately predicted by a machine learning model if the algorithm learns the current distribution of these differences beforehand.
Furthermore, the primary motivation behind LSTM networks is to remember the context of the information it explores for as long as possible. The network will have to "figure out" what context needs to be remembered on its own [2]. The overall configuration of the long short-term memory network is shown in Figure 6. The presented structure ( Figure 6) has an important feature necessary for solving the given task: at its core lies the forget gate (Figure 7), which autonomously determines which coordinates of the state vector need to be remembered [3].  Another important element in the LSTM architecture is the new state, which is formed in the LSTM model as a composition of the old state, modified by the gate, and the vector of values for new weight "candidates" (Figure 9). The final element in the neural network structure is the output state gate (Figure 10). It allows for the formation of the output prediction, which represents the predicted difference between the current prime number and the next unknown prime number, located at the "+1" position relative to the considered number. The software implementation of the LSTM architecture ( Figure 11) was based on 100 training epochs. The software algorithm allowed for accurate prediction of the next difference and determination of the prime number at the "+1" position relative to the considered number. For example, for the number "14081," the next prime number would be "14083," and thus, the difference between these numbers would be "2." The predictive model (Figure 12) yielded completely identical results.

Conclusion
From the obtained results of the machine learning model predictions, it can be concluded that determining the next difference between prime numbers is a solvable task from the perspective of approximation algorithms. In cases where an additional level of reliability is required, one can resort to verifying the predicted prime number using a primality test (factoring the number). This approach can increase the probability of classifying the predicted number as prime to unity (or reduce it to zero in the opposite case) and reduce the computational resources needed for exhaustive search through the entire sequence of natural numbers to find the next prime number.