Firstly, I have never found a mathematical formula for calculating primes in order. But after all, if there are algorithms, then it is certainly possible to compose formulas using logical functions or operators. I give below the most concise formula that turned out.
For some sequence of numbers we introduce the operator of detection of the first number equal to a:
All primes, starting with 5, can be calculated by the formula:
Operator iterates over the remainder of dividing each candidate number by simplicity with number to already found primes up to . Candidate numbers are selected in order from the set of odd numbers larger than the previous prime number . Is a pi function showing the number of primes .
Operator iterates over the output values ββof the operator until it finds 0. Since the series of primes is infinite, this will happen sooner or later. At the exit of the operator so there will always be some number i . Bottom line is determined by the maximum difference of adjacent primes smaller than the desired one. The increase in this difference occurs logarithmically. The graph below shows the dependence of the maximum and average growth from n for the first 100,000 primes. The maximum value was sampled and averaged for each thousand numbers.
The maximum increase in the difference of primes to previous maximum value equals 20 (for primes 3312 and 3384). It is in the region where the derivative of the envelope logarithm still large enough, and prime numbers are no longer too small. Based on this value, the condition for . Schedule for the first 50,000 primes given below. Values ββwere calculated for every thousand.
A peak is seen at 20. With increasing n, the graph goes to minus, showing a decrease in the growth rate of large primes.
The second consideration is to optimize the calculation of a sequence of primes.
The algorithm laid down in the formula above is an improved method for enumerating divisors. Improvements are to exclude even numbers from the consideration and check the divisibility of only primes smaller than sq. the roots of the candidate numbers. The most difficult part of the algorithm is the calculation of the set of remainder mod functions. The complexity can be reduced by optimizing this function. However, there is an even more effective way. Let be Is a sequence of residues from dividing the last found prime number into primes from 3 to the root of it. We will make sequences of the form
in order starting from . The last term is calculated if . When at some step of the calculation the remainder becomes equal to 0, go to the next sequence. This is done until i is found, at which all residues are nonzero. According to the recurrence formula above, this means finding the next prime number. Sequence it must be saved until the next prime number is found.
In the described algorithm, the operation mod is facilitated: divisible by times more dividers. The only exceptions are the occurrence of new simple divisors. When implementing the algorithm, it is necessary to store in the computer memory an array of primes to the root of the sought-for one, as well as a variable array of residuals. The complexity of the algorithm in the general sense (the amount of work) may be less than that of other known methods. The most complex operations in it are the extraction of the square root, the calculation of residues and multiplication. The root can be extracted to the nearest integer. To obtain residues, you can use an effective algorithm based on the general rule of divisibility. Multiplication is used only by 2 relatively small numbers i . The time complexity of the algorithm can be reduced by distributing the work according to the values ββof i . The segmented sieve obtained in this way should work faster on multi-threaded computers. However, the work performed will be larger due to the increase in dividends. You can also βscrewβ wheel factorization onto the algorithm. With the optimal size of the wheels, this can reduce complexity in a certain range of n - until the hardware "wilds" slow it down.
Perhaps someone will come in handy my thoughts.