português

A Prime-Generating Formula

To determine whether a number is prime, one can test divisibility by candidate factors up to its square root. If any division by an integer greater than 11 yields a zero remainder, then the number is not prime. Otherwise, it is prime.

To detect when a quotient is an integer, we introduce the function dm(n)d_m(n), defined for any integer mm. This function returns 11 when n=mn=m, and 00 otherwise:

dm(n)=⌊11+∣m−n∣⌋ d_m(n)=\left\lfloor \frac{1}{1+ \left| m-n \right| } \right\rfloor

When n≠mn \neq m, the denominator is greater than 11, and the expression evaluates to 00. When n=mn = m, the value is 11. The function dmd_m could also be defined for real values of mm and nn, and would behave in the same way, but this is not necessary for our purposes.

We now define a function P(n)P(n) that checks whether nn has any nontrivial integer factor. For each integer k=2,3,…,⌊n⌋k = 2, 3, \dots, \lfloor \sqrt{n} \rfloor, we compute the remainder of the division of nn by kk, given by n−k⌊nk⌋n - k \left\lfloor \frac{n}{k} \right\rfloor, and apply d0d_0:

P(n)=∏k=2⌊n⌋(1−d0(n−k⌊nk⌋)) P(n)=\prod_{k=2}^{\lfloor \sqrt{n}\rfloor} \left( 1 - d_0 \left(n - k \left\lfloor \frac{n}{k}\right\rfloor\right) \right)

For n≥4n \ge 4, all factors in this product are equal to 11 when nn is prime, so P(n)=1P(n)=1. If nn is composite, at least one factor is 00, which makes P(n)=0P(n)=0.

For n=1,2,3n = 1, 2, 3, the product is empty and therefore equal to 11. Since 11 is not prime, we introduce a correction factor:

z(n)=⌈n−P(n)n⌉ z(n)=\left\lceil\frac{n-P(n)}{n}\right\rceil

This expression is zero only when n=1n=1. We can now define a function that indicates primality:

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

This function returns 11 if nn is prime and 00 otherwise.

The prime-counting function π(n)\pi(n) is then given by:

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

We also define π(0)=0\pi(0)=0, corresponding to the empty sum.

Given an upper bound NN for the nn-th prime, we can construct a function that returns the nn-th prime by summing candidate values kk, weighted by a selector that picks out the first kk such that π(k)=n\pi(k)=n:

p(n)=∑k=1Nk⋅dn(π(k))⋅dn−1(π(k−1)) p(n)=\sum_{k=1}^{N} k \cdot d_n(\pi(k)) \cdot d_{n-1}(\pi(k-1))

This works because the nn-th prime is precisely the value of kk for which π(k)=n\pi(k)=n and π(k−1)=n−1\pi(k-1)=n-1.

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

N=⌊2+n⋅(ln⁡n+ln⁡(1+ln⁡n))⌋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 bound relies on the fact that p(n)p(n) is less than n⋅(ln⁡n+ln⁡(ln⁡n))n \cdot (\ln n+\ln(\ln n)) for n≥6n \ge 6, with smaller cases verifiable directly. The last grows very rapidly and serves as a guaranteed bound when no good estimate for p(n)p(n) is available.

This formula is not computationally efficient. It is simply a mathematical reformulation of the classical primality test based on 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).