A Formula That Generates Prime Numbers

An Equation That Summons Every Prime!

Dewan Mukto
4 min readMay 2, 2024

--

Photo by vackground.com on Unsplash

Foreword

While playing around with recurrence relationships, such as the world-renowned Fibonacci sequence, I decided to have a go at figuring out if the sequence of prime numbers could be represented as such a relationship, too. Indeed, I have heard mathematical urban legends about how people have wanted to find a sort of ‘pattern’ for accurately predicting prime numbers. Or if there exists such a formula that can always generate primes.

Fuelled by the curiosity to explore and the motivation to discover something new, I believe I have found something valuable today!

The Pattern

Here is what I found when writing down the prime numbers as a sum and difference of values in order to obtain the prime itself:

Sum & Difference Of 3 Integers

After the 5th prime (11), there is a visible pattern of adding the last prime with the 2nd last prime while subtracting the 3rd last prime.

The Formula

Here it is, in all its glory:

Recurrence Equation For Prime Number Generation

Dependent upon the following initial values corresponding to the first 5 primes:

Initialization Of The Equation

The Flaw

Like most algorithms or formulae that are supposed to generate primes, my formula, too, suffers from a minor problem. But one that can be cured, nonetheless!

If the equations are written in an alternative manner, with the primes being on the right-hand-side of the equations, then here are the next primes continuing from 13:

A Potential Flaw? Or No?

Some of the “generated primes” are multiples of prime factors, i.e. 25 = 5x5, 35 = 5x7, 49 = 7x7, etc. Which means they definitely are not prime!

They are still included and carried forward in the formulae since the successive primes rely on their values to be input into the recurrence equation. However, it can be easily mitigated by turning the formula into an algorithm and mentioning a conditional that “if any of the ‘generated primes’ are divisible by any of the preceding values, or the first 5 primes at least, ignore and move on”.

Following this recurrence formula by hand may appear a bit complicated for calculating larger primes, but for modern computers it is a simple walk in the park, since they are highly obedient machines who will follow through any objective if it is presented in the form of an algorithm.

To taste the power of this algorithm, simply copy-paste this sample Python script written by me for prototyping my hypothesis in a very simple way:

# Prime number generator
# @author Dewan Mukto
# @version 0.5.2.2024
def generate_primes(nmax):
x = [2, 3, 5, 7, 11] # initial values
n = 1 # counter of the current nth prime

# just spit out the preset values for n < 5
if nmax <= 5:
while n <= nmax:
print(x[n-1], end=", ")
n += 1
print()
# apply the "recurrence relationship" otherwise
else:
while n <= 5:
print(x[n-1], end=", ")
n += 1
while n <= nmax:
# apply the formula
xn = x[-1] + x[-2] - x[-3]
if not any(xn % xi == 0 for xi in x):
# if the Xn is not divisible by any of its previous "values"
# (see Note on my website https://diztil.github.io/research/mathematics/discovery/2024/05/02/prime-number-formula)
print(xn, end=", ")
x.append(xn)
n += 1
else:
x.append(xn)
n += 1
print()
# The "recurrence relationship": Xn = Xn-1 + Xn-2 - Xn-3
print("Welcome to Dewan Mukto's Prime Number Generator!")
nmax = int(input("Upto which prime to generate?: "))
generate_primes(nmax)

I am not sure if this formula can fully live up to its name, but if it does, I hope it carries mine! 😉️

Thanks for reading this!

--

--

Dewan Mukto

Hobbyist author & poet. Aspiring technocrat, full-time technologist, and part-time scientist.