português

Prime-Generating Formula

When dividing any positive integer nn by a positive integer dd, the result will be an integer if and only if dd is a divisor of nn. If nn is a prime number, its only divisors are 11 and nn itself. To determine whether a number nn is prime, it suffices to check whether the division results in an integer for divisors dd from 22 up to the greatest integer less than or equal to the square root of nn. For prime nn, none of the divisions by k[2,n]k \in [2,\lfloor \sqrt{n} \rfloor] yields an integer. If we multiply the fractional parts of all these divisions, the result will be zero only when at least one of the factors is zero, that is, when the number is composite. Therefore, the function below, for n>3n > 3, is zero only for composite numbers, and 11 otherwise:

P(n)=k=2n(nknk) P(n)=\left\lceil\prod_{k=2}^{\lfloor \sqrt{n}\rfloor}\left(\frac{n}{k}-\left\lfloor\frac{n}{k}\right\rfloor\right)\right\rceil

For nn from 11 to 33, the empty product is equal to 11. Since 11 is not prime, we can multiply the value by

z(n)=nP(n)n z(n)=\left\lceil\frac{n-P(n)}{n}\right\rceil

which is zero only for n=1n=1. Thus, the function below, defined for positive integers, returns 11 for any prime nn, and 00 otherwise:

f(n)=z(n)P(n) f(n)=z(n) \cdot P(n)

The function π(n)\pi(n) counts how many prime numbers exist from 11 to nn, using the function ff defined above:

π(n)=m=1nf(m) \pi(n)=\sum_{m=1}^{n} f(m)

The value of π(0)\pi(0) is defined as zero, corresponding to the empty sum.

Let us define a function dm(k)d_m(k) that equals 11 when k=mk=m, and 00 otherwise:

dm(k)=11+(mk)2 d_m(k)=\left\lfloor \frac{1}{1+(m-k)^2} \right\rfloor

When kmk \neq m, the denominator is greater than 11, and the entire expression evaluates to zero. Otherwise, it equals 11. Thus, dn(k)d_n(k) equals 11 only when k=nk=n, for a fixed nn.

If we have an upper bound NN for the nn-th prime, we can construct a function that returns the nn-th prime by summing the candidates kk that may be the nn-th prime, multiplied by a function that selects only the first kk such that π(k)=n\pi(k)=n.

p(n)=k=1Nkdn(π(k))dn1(π(k1)) p(n)=\sum_{k=1}^{N} k \cdot d_n(\pi(k)) \cdot d_{n-1}(\pi(k-1))

When the index kk satisfies π(k)=n\pi(k)=n and π(k1)=n1\pi(k-1)=n-1, we have found exactly the nn-th prime.

Since p(n)p(n) grows approximately as nlnnn \ln n, we can use upper bounds such as:

N=2+n(lnn+ln(1+lnn))N=n2+1N=22n \begin{array}{l} N = \left\lfloor 2+n \cdot (\ln n + \ln \left( 1 + \ln n \right)) \right\rfloor \\ N = n^2+1 \\ N = 2^{2^n} \end{array}

The first is based on the fact that p(n)p(n) is less than n(lnn+ln(lnn))n \cdot (\ln n + \ln(\ln n)) for n6n \geq 6, with smaller cases verifiable directly. The last grows very rapidly and serves as a guarantee when one does not have a good estimate of the value of the prime p(n)p(n).

Obviously, the formula is not efficient. It is merely a mathematical notation for the primality testing algorithm that uses successive divisions up to the square root of the number being tested.

This formula was created (discovered?) on 2026-05-04 by José Eduardo Gaboardi de Carvalho (edu.a1978 at gmail).