## Introduction

The algorithm for calculating prime numbers is based on the idea of a prime number as the movement of such numbers. The basic idea is that prime numbers starting with 5 are not static, but dynamic, and can only appear in strictly defined places (6n ± 1). Thus, each new prime number, appearing, begins to move and occupy these places, preventing new prime numbers from appearing, since they will be derivatives of another prime number. To calculate the free space for the new estimated primes, which are formed by adding 2 and 4 to the initial one (5) in turn, division with the remainder is used for all previously stored primes, and if at least once the remainder is 0, then the place is taken, otherwise it is a new prime. Thus, the algorithm shows that primes do not appear randomly, but can be calculated from the previous ones.

## Background

More details about the nature of primes can be found in my research Prime Number Explanation. Sorry for the quality, no degree, did by hand.

## Using the Code

The code can be copied and pasted into the main method, to get the desired number of primes, change the cycle limit and run it.

bool free;
int factor = -1;
var primes = new List<uint>();
primes.Add(5);
uint next = primes.First();
for (int i = 0; i < 5000000; i++)
{
free = true;
next += factor < 0 ? 2U : 4U;
foreach(var p in primes)
{
if (next - p < p) break;
if (next % p == 0) { free = false; break; }
}
if (free) primes.Add(next);
factor -= 2 * factor;
}
Console.WriteLine("The last prime: " + primes.Last());
Console.WriteLine("The count of primes: " + primes.Count());
Console.ReadKey();

The opposite way of finding primes, the more reflective the idea, but extremely inefficient. In contrast to the previous one, it moves forward on a numerical line, as if running into the future. The found prime (6n±1) is extended by calculating all the numbers that will be at 6n±1 positions. These numbers are placed in the set, so only 6n±1 numbers will be in it, other numbers do not matter, because they will always be divisible by 2 or 3. The next 6n±1 number after the largest found prime is checked for presence in the set, and if it is there, then it is composite, otherwise it is prime.

var composites = new HashSet<uint>();
var primes = new List<uint>();
int factor = -1;
uint next = 5;
int limit = 1000;
while(next < limit)
{
if (! composites.Contains(next))
{
uint temp = next;
while (temp < limit)
{
temp += next * 4;
composites.Add(temp);
temp += next * 2;
composites.Add(temp);
}
primes.Add(next);
}
next += factor < 0 ? 2U : 4U;
factor -= 2 * factor;
}

## Points of Interest

In general, it is very funny, it is like a road along which prime numbers move discretely. And the new number needs to cross this road, as it were, and do it only in a strictly designated place, and if possible immediately after the largest number, so that it does not collide with others. And as soon as this transition is found, the number occupies it, the road becomes wider, and the traffic is more intense than making the transition more difficult.

These ways are ineffective and have rather theoretical interest.

## History

- 17
^{th} October, 2019: Initial version - 21
^{st }October, 2019: Second version (added the opposite way; omitted the efficiency question)

This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.