Prime numbers – mysterious, fascinating and inseparable from the history of mathematics. From ancient times to modern times, mathematicians have been trying to solve their riddles, thus discovering many important theories and theorems.
The information presented below will allow us to learn the principle of how prime numbers are created and to predict their error-free occurrence without any actions related to the primality test. In short, we will learn the long-sought prime number code.
PRIME NUMBERS PATTERN
The following formula defines the set of prime numbers:
\( P = (A \cup B) \setminus C \)
\( where: \)
\( \mathbb{A} = \{ 2, 3 \} \)
\( \mathbb{B} = \{ 6k±1 \mid k \in \mathbb{N}, k \geq 1, \lim_{k \to \infty} \} \)
\( \mathbb{C} = \left\{ p \cdot q \mid p,q \in \mathbb{B} \right\} \)
Below is the optimized formula for determining prime numbers for a given range:
\( P = (A \cup B \cup C) \setminus (D \cup E \cup F) \)
\( where: \)
\( \mathbb{A} = \{ 2, 3, 5 \} \)
\( \mathbb{B} = \{ 6k+1 \mid k \in \mathbb{N}, k \geq 1, k \leq \frac{x}{6}, k \mod 10 \notin \{ 4, 9 \} \} \)
\( \mathbb{C} = \{ 6k-1 \mid k \in \mathbb{N}, k \geq 2, k \leq \frac{x}{6}, k \mod 10 \notin \{ 1, 6 \} \} \)
\( \mathbb{D} = \{ (6k_1 + 1)(6k_2-1) \mid k_1, k_2 \in \mathbb{N}, k_1 \geq 1, k_2 \geq 2, k_1 \leq \frac{x}{42}, k_2 \leq \frac{x}{6(6k_1 + 1)}, k_1 \mod 10 \notin \{ 4, 9 \}, k_2 \mod 10 \notin \{ 1, 6 \} \} \)
\( \mathbb{E} = \{ (6k_1 + 1)(6k_2 + 1) \mid k_1, k_2 \in \mathbb{N}, k_1 \geq 1, k_2 \geq 1, k_1 \leq \frac{x}{42}, k_2 \leq \frac{x}{6(6k_1 + 1)}, k_1 \mod 10 \notin \{ 4, 9 \}, k_2 \mod 10 \notin \{ 4, 9 \} \} \)
\( \mathbb{E} = \{ (6k_1-1)(6k_2-1) \mid k_1, k_2 \in \mathbb{N}, k_1 \geq 2, k_2 \geq 2, k_1 \leq \frac{x}{66}, k_2 \leq \frac{x}{6(6k_1 – 1)}, k_1 \mod 10 \notin \{ 1, 6 \}, k_2 \mod 10 \notin \{ 1, 6 \} \} \)
Python script that reflects the above formula for x = 1,000,000:
x = 1000000
def generate_sieve(x):
sieve = [False] * (x + 1)
A = {2, 3, 5}
for num in A:
if num <= x:
sieve[num] = True
k_B_max = y + 1
for k in range(1, k_B_max):
if k % 10 not in {4, 9}:
val = 6 * k + 1
if val <= x:
sieve[val] = True
k_C_max = y + 2
for k in range(2, k_C_max):
if k % 10 not in {1, 6}:
val = 6 * k - 1
if val <= x:
sieve[val] = True
k_DE_max = x // 42
for k1 in range(1, k_DE_max):
if k1 % 10 in {4, 9}:
continue
n = 6 * k1 + 1
max_k2 = x // n + 2
for k2 in range(1, max_k2):
if k2 % 10 not in {1, 6}:
val_D = n * (6 * k2 - 1)
if val_D <= x:
sieve[val_D] = False
if k2 % 10 not in {4, 9}:
val_E = n * (6 * k2 + 1)
if val_E <= x:
sieve[val_E] = False
k_F_max = x // 66
for k1 in range(2, k_F_max):
if k1 % 10 in {1, 6}:
continue
n = 6 * k1 - 1
max_k2 = x // n + 2
for k2 in range(2, max_k2):
if k2 % 10 in {1, 6}:
continue
val = n * (6 * k2 - 1)
if val <= x:
sieve[val] = False
return sieve
y = x // 6
sieve = generate_sieve(x)
P = {i for i, is_true in enumerate(sieve) if is_true}
with open('prime.txt', 'w') as file:
for number in sorted(P):
file.write(f"{number}\n")
Simplified version of the script:
x = 1000000
def generate_set_B(x):
B = set()
for k in range(1, k_B_max):
if k % 10 not in {4, 9}:
val = 6 * k + 1
if val <= x:
B.add(val)
return B
def generate_set_C(x):
C = set()
for k in range(2, k_C_max):
if k % 10 not in {1, 6}:
val = 6 * k - 1
if val <= x:
C.add(val)
return C
def generate_sets_D_and_E(x):
D, E = set(), set()
for k1 in range(1, k_DE_max):
if k1 % 10 in {4, 9}:
continue
n = 6 * k1 + 1
max_k2 = k_max * n + 2
for k2 in range(1, max_k2):
if k2 % 10 not in {1, 6}:
val_D = n * (6 * k2 - 1)
if val_D <= x:
D.add(val_D)
if k2 % 10 not in {4, 9}:
val_E = n * (6 * k2 + 1)
if val_E <= x:
E.add(val_E)
return D, E
def generate_set_F(x):
F = set()
for k1 in range(2, k_F_max):
if k1 % 10 in {1, 6}:
continue
n = 6 * k1 - 1
max_k2 = x // 6 * n + 2
for k2 in range(2, max_k2):
if k2 % 10 in {1, 6}:
continue
val = n * (6 * k2 - 1)
if val <= x:
F.add(val)
return F
def save_to_file(filename, data):
with open(filename, 'w') as file:
for number in sorted(data):
file.write(f"{number}\n")
k_max = x // 6
k_B_max = k_max + 1
k_C_max = k_max + 2
k_DE_max = x // 42
k_F_max = x // 66
A = {2, 3, 5}
B = generate_set_B(x)
C = generate_set_C(x)
D, E = generate_sets_D_and_E(x)
F = generate_set_F(x)
P = (A | B | C) - (D | E | F)
save_to_file('prime.txt', P)
Set A contains the numbers 2, 3 and 5.
The set B contains numbers of the form 6k+1 with some exceptions up to a fixed range k.
The set C contains numbers of the form 6k-1 with some exceptions up to a fixed range k.
The set D contains the products of all possible combinations of numbers from the patterns 6k-1 and 6k+1 up to a fixed range k.
The set E contains the products of all possible combinations of numbers from the patterns 6k+1 up to a fixed range x.
The set F contains the products of all possible combinations of numbers from the patterns 6k-1 up to a fixed range k.
As we can see, after removing from sets A∪B∪C all numbers that are in sets D∪E∪F we obtain a set P that contains only prime numbers up to a fixed range x.
I took the liberty of calling the numbers from the sets D∪E∪F near-prime numbers, which we will learn more about in the NEAR-PRIME NUMBERS section.
Since there are formulas for the sets of numbers (A∪B∪C) and (D∪E∪F), it becomes logical that the set of prime numbers P can also be expressed as a formula.
To illustrate these assumptions in an easy graphical way, we will use the Ulam spiral, which is represented by the formula:
\( P = (A \cup B \cup C) \setminus (D \cup E \cup F) \)
Then we create spirals with the products of prime numbers of the form 6k±1, i.e. with numbers from the set:
\( (D \cup E \cup F) = (A \cup B \cup C) \setminus P \)

Now we superimpose the Ulam spirals on the spirals with numbers from the sets D∪E∪F. The result can be seen below:

We obtained spirals with a symmetrical arrangement of prime and near-prime numbers, i.e. with numbers from the formula:
\( (A \cup B \cup C) = P \cup (D \cup E \cup F) \)
This symmetrical spiral structure proves that the occurrence of prime numbers is completely predictable and that prime numbers can be represented with full certainty in the form of the following pattern:
\( P = (A \cup B \cup C) \setminus (D \cup E \cup F) \)
An important aspect of the existence of a relatively symmetrical spiral with numbers in the form 6k±1 is the fact that we can not only more easily predict the future locations of prime numbers, but we also have an easier possibility of factoring near-prime numbers which are the values of n in the RSA algorithm. Details coming soon, and as an incentive, I present a prime number of 101 million digits available for download here.
Alternative version of the formula:
\( P = (A \cup B \cup C) \setminus (D \cup E) \)
\( where: \)
\( \mathbb{A} = \{ 2, 3, 5 \} \)
\( \mathbb{B} = \{ 6k+1 \mid k \in \mathbb{N}, k \geq 1, k \leq \frac{x}{6}, k \mod 10 \notin \{ 4, 9 \} \} \)
\( \mathbb{C} = \{ 6k-1 \mid k \in \mathbb{N}, k \geq 2, k \leq \frac{x}{6}, k \mod 10 \notin \{ 1, 6 \} \} \)
\( \mathbb{D} = \left\{ p^2 + p \cdot n \mid p \in \mathbb{B}, \quad p < \sqrt{\max(\mathbb{B})}, \quad (p^2 + p \cdot n) \bmod 10 \notin \{ 5 \}, \quad n \in 2\mathbb{N}_0 \setminus (2 + 6\mathbb{N}) \right\} \)
\( \mathbb{E} = \left\{ p^2 + p \cdot n \mid p \in \mathbb{C}, \quad p < \sqrt{\max(\mathbb{B})}, \quad (p^2 + p \cdot n) \bmod 10 \notin \{ 5 \}, \quad n \in 2\mathbb{N}_0 \setminus (4 + 6\mathbb{N}) \right\} \)
Python script that reflects the above formula for x = 1,000,000:
x = 1000000
z = x // 6 + 2
y = x // 6 + 1
def generate_B(x, y):
B = set()
for k in range(1, y):
p = 6 * k + 1
if k % 10 not in {4, 9}:
B.add(p)
return B
def generate_C(x, z):
C = set()
for k in range(2, z):
p = 6 * k - 1
if k % 10 not in {1, 6}:
C.add(p)
return C
def valid_n_values(x, step, offset):
return {n for n in range(0, x + 1, 2) if n not in {offset + 6 * k for k in range(z)}}
valid_n_D = valid_n_values(x, 2, 2)
valid_n_E = valid_n_values(x, 2, 4)
def generate_D(x, B):
max_B = max(B)
limit_p = int(max_B**0.5)
D = set()
for p in B:
if p < limit_p:
for n in valid_n_D:
value = p**2 + p * n
if value <= max_B and value % 10 != 5:
D.add(value)
return D
def generate_E(x, C):
max_C = max(C)
limit_p = int(max_C**0.5)
E = set()
for p in C:
if p < limit_p:
for n in valid_n_E:
value = p**2 + p * n
if value <= max_C and value % 10 != 5:
E.add(value)
return E
A = {2, 3, 5}
B = generate_B(x, y)
C = generate_C(x, z)
D = generate_D(x, B)
E = generate_E(x, C)
P = (A | B | C) - (E | D)
with open('prime.txt', 'w') as file:
for number in sorted(P):
file.write(f"{number}\n")
Modified Sieve of Eratosthenes
Let us first determine two sets of numbers in which more than \( \frac{2}{3} \) natural numbers are omitted and which determine all prime numbers (except 2 and 3) and also determine the near-primes:
\( B = \{ 6k+1 \mid k \in \mathbb{N}, k \geq 1, k \leq x \} \)
\( C = \{ 6k-1 \mid k \in \mathbb{N}, k \geq 1, k \leq x \} \)
Numbers from set B:
{7, 13, 19 ,25, 31, 37, 43, 49, 55, 61, 67, 73, 79, 85, 91, 97, 103, 109, 115, 121, 127, 133, 139, 145, 151, 157, 163, 169, 175, 181, 187, 193, 199, 205, 211, 217, 223,…}
Numbers from set C:
{5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 83, 89, 95, 101, 107, 113, 119, 125, 131, 137, 143, 149, 155, 161, 167, 173, 179, 185, 191, 197, 203, 209, 215, 221,…}
An interesting fact is that 50% of all numbers in the Fibonacci sequence are numbers from the discussed 6k±1 patterns, more information in the FIBONACCI SEQUENCES section.
Determining consecutive prime numbers using trigonometric functions.
As we can see in the image below, we place our two sets of numbers on the X coordinate axis. From the point of intersection of the X and Y axes to the left side we place numbers from the pattern 6k-1, while to the right side we place numbers from the pattern 6k+1.

At the very beginning, I would like to emphasize that the operations described below must always start from the number “5” and perform them gradually on the next number greater than the previous one, i.e. 5, 7, 11, 13, 17, 19, 23, 25… etc.
We determine from our smallest number, i.e. from the number “5” a sine wave that intersects the X axis every 5 positions. In the image below we can see the effect of this operation:

As we can see above, all natural numbers on our X-axis greater than the number from which we determined the last sine wave (in this case greater than the number 5) and through which the sine wave runs are always numbers that are not prime numbers, because they are products or squares of prime and/or near-prime numbers.
Predicting the occurrence of prime numbers.
All natural numbers on our X axis greater than the numbers from which we have already determined the sine wave, and at the same time less than the value of the square of the next number are prime numbers if no sine wave intersects them.
Additionally, all natural numbers on our X axis less than or equal to the value from which we determined the last sine wave that are intersected by only one sine wave are also prime numbers.
So, as we can see in the image below, in this case all numbers greater than 7 and less than the square of the next number, i.e. 112 = 121, which are not intersected by any sine wave, are prime numbers. Additionally, all numbers less than or equal to the number from which we determined the last sine wave, i.e. the number “7” which is intersected by only one sine wave, are also prime numbers.

We use this method strictly following the rule that we always start the calculations from the number “5“, determining the sine wave that intersects the X axis every five positions, from the number “7” the sine wave intersecting the X axis every seven positions, from the number “11” the sine wave intersecting the X axis every eleven positions, from the number x the sine wave intersecting the X axis every x positions.
It should be emphasized that although the above method has the characteristics of the Sieve of Eratosthenes, it only operates on \( \frac{1}{3} – 1 \) natural numbers (because we omit \( \frac{2}{3} \) natural numbers and the number 1). Additionally, it is distinguished by the fact that it correctly defines subsequent prime numbers smaller than the value of the square of the next number, without the need for any primality tests. The higher the target value of x, the fewer logarithmically we have to perform sine wave drawings, because the difference in the number of numbers between the number x and its root grows exponentially.
To find primes up to \(x\), it is necessary to determine the sinusoids of to the numbers up to \( \sqrt{x} \) . This means that all that needs to be generated is \( \frac{\sqrt{x}}{3} – 1 \) sinusoids.
For example, if we want to obtain prime numbers in the range \( x \leq 1,000,000 \), it is enough to generate sinusoids from numbers up to \( \sqrt{1,000,000} \), i.e. up to \( 1000 \) inclusive. Consequently, we only need to generate \( \frac{\sqrt{1\,000\,000}}{3} – 1 \approx 332 \) sine waves. As a result, after performing just \( \approx 332 \) operations, we can obtain a set of prime numbers in the range from 5 to 1,000,000.
Below we have a simple script written in Python which illustrates the discussed principle of determining prime numbers and generates in the prime.txt file without errors all prime numbers up to the value max_num = 1000001 which must be in the form of 6k±1
max_num = 1000001 # the number must be 6k±1
y = max_num - 1
x = -max_num
sieve = list(range(x, y, 6))
marked = [True] * len(sieve)
sqrt_max = max_num ** 0.5
for i, number in enumerate(sieve):
if number == 1 or number == -1:
marked[i] = False
for i, number in enumerate(sieve):
if number == 1 or number == -1 or abs(number) > sqrt_max:
continue
offset = abs(number)
for j in range(i + offset, len(sieve), offset):
marked[j] = False
for j in range(i - offset, -1, -offset):
marked[j] = False
result_numbers = [abs(sieve[i]) for i, mark in enumerate(marked) if mark]
result_numbers = sorted(result_numbers)
with open("prime.txt", "w") as file:
file.write("2\n3\n")
for number in result_numbers:
file.write(f"{number}\n")
Below we have a modified Sieve of Eratosthenes written in Python, which only operates on numbers from the sets of 6k±1, which speeds up the determination of subsequent prime numbers:
import numpy as np
max_num = 1000000
def generate_prime(max_num):
prime = np.zeros(max_num + 1, dtype=np.bool_)
prime[2] = True
prime[3] = True
prime[ np.arange(5, max_num + 1, 6) ] = True
prime[ np.arange(7, max_num + 1, 6) ] = True
limit = int(np.sqrt(max_num))
p = 5
while p <= limit:
if prime[p]:
prime[p*p::2*p] = False
p += 6
p = 7
while p <= limit:
if prime[p]:
prime[p*p::2*p] = False
p += 6
return prime
prime = generate_prime(max_num)
prime_numbers = [str(i) for i in range(2, max_num + 1) if prime[i]]
with open('prime.txt', 'w') as file:
file.write("\n".join(prime_numbers))
import numpy as np
max_num = 1000000
def generate_sequence(max_num):
max_num_6 = max_num // 6
sqrt_max_num = int(np.sqrt(max_num))
sieve = np.ones(max_num + 1, dtype=np.bool_)
target_numbers = []
for k in range(1, max_num_6 + 1):
num1 = 6 * k - 1
num2 = 6 * k + 1
if num1 <= max_num:
target_numbers.append(num1)
if num2 <= max_num:
target_numbers.append(num2)
for num in target_numbers:
if num <= sqrt_max_num and sieve[num]:
start = num * num
step = 2 * num
sieve[start:max_num + 1:step] = False
prime_numbers = [num for num in target_numbers if sieve[num]]
with open('prime.txt', 'w') as f:
f.write("2\n3\n")
f.write("\n".join(map(str, prime_numbers)))
primes = generate_sequence(max_num)
HYPOTHESES
Using the patterns 6k±1 limits the search for primes to just \( 26\frac{2}{3}\% \) of all natural numbers (assuming that we eliminate from our set numbers greater than 5 that end with the digit 5).
All prime numbers greater than 3 have the form 6k±1.
All prime squares (greater than 3) always have the form 6k+1.
The squares of primes (except 2 and 5) always end with the digit 1 or 9.
The product of primes having the form 6k+1 always appears only in the set 6k+1.
The product of primes having the form 6k-1 also always appears only in the set 6k+1.
If one prime is of the form 6k+1 and the other is 6k-1, then their product is of the form 6k-1.
Every prime number (greater than 3) is an odd number and its sum of digits can only be
1, 2, 4, 5, 7 or 8.
Prime numbers in the form 6k-1 have the sum of their digits 2, 5 or 8, while numbers in the form 6k+1 have the sum of their digits 1, 4 or 7.
The sum of the digits of a prime number (except for the number 3) will never be 3, 6 or 9.
Let me remind you that Nikola Tesla considered the numbers 3, 6 and 9 to be exceptional.
It is worth noting that the near-prime numbers create the value of the n exponent of the public key in the RSA encryption algorithm. More information in the RSA section.